简体   繁体   English

为什么extjs按钮处理程序不起作用

[英]Why extjs button handler doesn't work

I have a window containing some button after clicking 'cancel' nothing happens. 单击“取消”后,我有一个包含一些按钮的窗口没有任何反应。 I'm really confused what could I'm getting wrong: 我真的很困惑我可能会出错:

Ext.define('Myapp.view.User.NewForm', {
    extend: 'Ext.window.Window',
    width: 610,
    height: 380,
    y: 50,
    resizable: false,
    closable: true,
    draggable: false,
    title: 'new user',

    items: [{
        buttons: [{
                text: 'save',
                iconCls: 'form-save'
            },{
                text: 'cancel',
                iconCls: 'form-cancel',
                scope: this,
                handler: this.cancel     
            }]
    }],
    cancel: function() { alert('cancel'); }

})

Like Lolo says, this.cancel is undefined at the time when you define your Form class. 就像Lolo所说,在定义Form类时, this.cancel是未定义的。

The standard solution for this problem is to create items array inside initComponent : 解决此问题的标准方法是在initComponent内创建items数组:

Ext.define('Myapp.view.User.NewForm', {
    ...

    initComponent: function() {
        this.items = [{
            buttons: [{
                text: 'save',
                iconCls: 'form-save'
            },{
                text: 'cancel',
                iconCls: 'form-cancel',
                scope: this,
                handler: this.cancel     
            }]
        }];

        this.callParent();
    },

    cancel: function() { alert('cancel'); }

});

When initComponent is invoked this points to the instance of your Form as one would expect. 当调用initComponentthis指向您期望的Form的实例。 In you code this pointed to the global window object which didn't contain the cancel function. 在您的代码中, this指向不包含cancel函数的全局window对象。

You can also define your window buttons this way 您也可以通过这种方式定义窗口按钮

...
initComponent: function(){
this.buttons =  [
            {   text:'Close',
                handler: function(){
                    this.up('window').close(); //<--Notice "this" refers to the button
                }
            },
            {
                text: 'Save',
                action: 'save',
                handler: this.save,
                scope: this
            }
        ]; //eo buttons
        this.callParent(arguments);
    }, //eo initComponent
    save: function(){ ... }
...

That's because this.cancel is undefined. 那是因为this.cancel未定义。 See this code: 看到这段代码:

var that = this;
Ext.define('Myapp.view.User.NewForm', {

    items: [{
        buttons: [{
                text: 'save',
                iconCls: 'form-save'
            },{
                text: 'cancel',
                iconCls: 'form-cancel',
                scope: this, // that === this
                handler: this.cancel     
            }]
    }],
    cancel: function() { alert('cancel'); }
})

This passed to scope points to the same object as that variable. 这传递给范围指向与该变量相同的对象。 You must find other way to get reference to the parent control. 您必须找到其他方式来获取对父控件的引用。 You may try: handler: function(){ this.ownerCt.ownerCt.cancel.apply(this, arguments); } 您可以尝试: handler: function(){ this.ownerCt.ownerCt.cancel.apply(this, arguments); } handler: function(){ this.ownerCt.ownerCt.cancel.apply(this, arguments); } , and remove scope: this line. handler: function(){ this.ownerCt.ownerCt.cancel.apply(this, arguments); } ,并删除scope: this行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM