使用ExtJS构建登录getView不是一个函数

时间:2014-09-22 08:01:33

标签: extjs extjs5

我尝试使用带有PHP的extjs创建登录但我坚持这个问题......

TypeError: this.getView is not a function


this.getView().destroy();

这是我的代码......

我不知道为什么它可能是错误我只是尝试按照sencha docs中的教程

 buttons: [{
            text: 'Login',
            formBind: true,

        listeners: {
           click: function() {
                                        var form = Ext.getCmp('login').getForm();

                                        if(form.isValid()) {
                                                form.submit({
                                                        url : 'data/login.php',
                                                        method : 'POST',                                                      

                                                           success:function(){ 
                                                            this.getView().destroy();
                                                            Ext.widget('app-main');
                                                                },
                                                            failure: function() {
                                                                      obj = Ext.util.JSON.decode(response.responseText); 
                                                                        Ext.Msg.alert('Login Failed!', obj.errors); 
                                                            }
                                                });
                                        }
                               }              
        }
        }]   

1 个答案:

答案 0 :(得分:2)

你试过console.log(this)吗?我猜不是......

this始终依赖于上下文。这里的上下文不是视图,但是,如果我没有弄错的话,Ext.Ajax.request,你试图破坏视图的success函数。

这就是为什么我不会在我的代码中使用它,即使对于一个外行读者来说,即使对于我所处的上下文,也不会完全显而易见。

编辑:由于代码在评论中看起来很糟糕,我在这里添加它。 根据原始Ext源的答案是

click: function() {
    var me = this;
    var form = Ext.getCmp('login').getForm();

    if(form.isValid()) {
        form.submit({
            url : 'data/login.php',
            method : 'POST',

            success:function(){ 
                me.getView().destroy();
                Ext.widget('app-main');
            },
            failure: function() {
                obj = Ext.util.JSON.decode(response.responseText);
                Ext.Msg.alert('Login Failed!', obj.errors);
            }
        });
    }
}

虽然我个人会使用

click: function(btn) {
    var form = Ext.getCmp('login').getForm();

    if(form.isValid()) {
        form.submit({
            url : 'data/login.php',
            method : 'POST',

            success:function(){ 
                btn.up('window').hide();
                Ext.widget('app-main');
            },
            failure: function() {
                obj = Ext.util.JSON.decode(response.responseText);
                Ext.Msg.alert('Login Failed!', obj.errors);
            }
        });
    }
}