如何在不相关的ViewModel中设置数据

克里斯·施密茨(Chris Schmitz)

我有一个登录过程,我已经把它弄成小提琴了(我卡住的部分从第110行开始)。

这是代码的副本:

Ext.define('MyApp.MyPanel',{
    extend: 'Ext.panel.Panel',
    title: 'My App',
    controller: 'mypanelcontroller',
    viewModel: {
        data:{
            email: 'Not signed in'
        }  
    },
    width: 500,
    height: 200,
    renderTo: Ext.getBody(),

    bind:{
        html: "Logged in as: <b>{email}</b>"        
    },

    buttons:[
        {
            text: 'Sign in',
            handler: 'showSignInWindow'
        }
    ]
});

Ext.define('MyApp.MyPanelController',{
    extend: 'Ext.app.ViewController',
    alias: 'controller.mypanelcontroller',

    showSignInWindow: function (b,e,eOpts){
        Ext.widget('signinwindow').show();
    }
});

Ext.define('MyApp.SignInWindow',{
    extend: 'Ext.window.Window',
    title: 'Sign in',
    controller: 'signincontroller',

    xtype: 'signinwindow',

    width: 400,
    title: 'Sign In',
    modal: true,
    layout: 'fit',

    items:[
        {
            xtype: 'form',
            reference: 'signinfields',
            layout: 'anchor',
            bodyPadding: 5,
            defaults: {
                anchor: '100%',
                xtype: 'textfield'
            },
            items:[
                {
                    fieldLabel: 'Email',
                    name: 'email',
                    allowBlank: false
                },
                {
                    fieldLabel: 'Password',
                    name: 'password',
                    allowBlank: false,
                    inputType: 'password'
                }
            ],
            buttons:[
                {
                    text: 'forgot password',
                    width: 120,
                    //handler: 'onForgotPassword'
                },
                '->',
                {
                    text: 'sign in',
                    width: 120,
                    handler: 'onSignIn'
                }
            ]
        }
    ]
});


Ext.define('MyApp.SignInController',{
    extend: 'Ext.app.ViewController',
    alias: 'controller.signincontroller',

    onSignIn: function (button, event, eOpts){
        var data = button.up('form').getValues();
        button.up('window').mask('Signing in...');

        Ext.Ajax.request({
            // sorry, I don't know how to fake an API response yet :/
            url: '/authenticate/login',
            jsonData: data,
            scope: this,
            success: function (response){

            var result = Ext.decode(response.responseText);


            if(result.loggedIn == true){


                /*
                This is where I need help. 
                From the sign in window, I would like to update the viewmodel in `MyApp.MyPanel` with the 
                email returned in the response. If the window was a child of MyPanel, I would be able to update 
                via the ViewModel inheritance, but I can't here because the window isn't part of the `items` config.
                */
                this.getViewModel().set('email', result.data[0].email);

                Ext.toast({
                    title: 'Sign in successful',
                    html: "You've been signed in.",
                    align: 't',
                    closable: true,
                    width: 300
                });

                button.up('window').destroy();
            } else {
                Ext.toast({
                    title: 'Sign in failed',
                    html: "You sign in failed: " + result.message,
                    closable: true,
                    width: 300,
                    align: 't'
                });
                button.up('window').unmask();
            }

            },
            failure: function (){
            // debugger;

            }
        })
    },


    onForgotPassword: function (){
        Ext.Ajax.request({
            url: '/authenticate/test',
            success: function (response){

            },
            failure: function (){
            }
        })
        // Ext.Msg.alert('trigger forgot password logic', "This is where you need to trigger the API to send the forgot email form. <br>Say something here about how you'll get an email");
    }
});

Ext.application({
    name : 'Fiddle',

    launch : function() {
        Ext.create('MyApp.MyPanel');
    }
});

我正在尝试做的是:

  • 显示带有登录按钮的面板
  • 单击该按钮将显示一个登录窗口
  • 提交登录表单会尝试对服务器进行身份验证
  • 成功通过身份验证后,将在初始面板的ViewModel中设置用于用户的电子邮件

最后一个问题是我遇到的问题。

如果登录窗口是面板的子级,则可以通过ViewModel继承进行设置,但是由于我使用的是小部件显示,因此无法通过面板的项目配置重新设置。

有没有办法正确地做到这一点?

克里斯·施密茨(Chris Schmitz)

没关系,我想通了。答案一直是我的问题:

如果登录窗口是面板的子级,则可以通过ViewModel继承进行设置,但是由于我使用的是小部件显示,因此无法通过面板的项目配置重新设置。

只需将窗口作为面板的子代即可:

Ext.define('MyApp.MyPanelController',{
    extend: 'Ext.app.ViewController',
    alias: 'controller.mypanelcontroller',

    showSignInWindow: function (b,e,eOpts){

        // Instead of creating the widget and showing it, create it and add it to the panel.
        // The window is going to float anyway, so being a child of the panel is no big deal

        var signinwindow = Ext.widget('signinwindow');
        this.getView().add(signinwindow).show();
    }
});

这样做意味着窗口继承了面板的视图模型,您可以像这样设置视图模型数据:

Ext.define('Registration.view.signin.SignInController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.signin-signin',

    onSignIn: function (button, event, eOpts){
        var data = button.up('form').getValues();
        button.up('window').mask('Signing in...');

        Ext.Ajax.request({
            url: '/authenticate/login',
            jsonData: data,
            scope: this,
            success: function (response){
            debugger;
            var result = Ext.decode(response.responseText);


            if(result.loggedIn == true){

                // Now that the window has inherited the panel's viewmodel
                // you can set it's data from the windows controller
                this.getViewModel().set('username', result.data[0].eMail);
            ...

愤怒的表情符号:/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python中附加/连接不相关的数据帧

如何在SQL中检查多个不相关的条件?

如何加快不相关数据的for循环?

(Ember.js)如何在不相关的资源/路由之间共享模型数据?

如何在角度不相同的路径之间的不相关组件之间同步数据(rxjs 6)

我如何从两个表中搜索数据,它们不相关

如何从父表中选择与子一中的外键不相关的数据

如何在Wordpress中创建与任何页面都不相关的菜单标题?

如何在Power BI中使用DAX在不相关表中搜索字符串?

如何在Gridview Yii2中加入不相关的表并显示

如何在多阶段 docker build 中跳过不相关的步骤

如何在不同的、不相关的类中调用具有相同名称的方法?

如何处理数据集中不相关的图像

MySQL检查2表中不相关的数据

如何消除代码中不相关的输出?

如何使用git合并到不相关的分支中?

防止不相关的mysql数据

在不相关的组件之间传递数据

如何在一个sql查询语句中获取彼此不相关的两列数据?

在不相关的 ViewModel 中从我的身份验证类订阅用户变量 - Swift/Combine

如何隐藏不相关的ShellCheck消息?

雄辩。如何获取不相关的表

如何删除不相关的行-R?

如何对不相关的内容进行分组

如何在Symfony2中使用一堆不相关的实体创建表单?

如何识别HTML树中语义相关但结构上不相关的节点

如何从大型数据集中删除不相关的文本数据

如何在同一项目中将vue-cli与现有的不相关的webpack设置集成?

如何仅使互不相关的少数数据类型被泛型接受