Javascript 为什么extjs按钮处理程序不';行不通
单击“取消”后,我有一个包含一些按钮的窗口,但什么都没有发生。 我真的很困惑我到底错在哪里:Javascript 为什么extjs按钮处理程序不';行不通,javascript,extjs,extjs4,Javascript,Extjs,Extjs4,单击“取消”后,我有一个包含一些按钮的窗口,但什么都没有发生。 我真的很困惑我到底错在哪里: 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',
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'); }
})
这是因为this.cancel未定义。请参阅此代码:
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'); }
})
传递给作用域的对象指向与该变量相同的对象。必须找到其他方法来获取对父控件的引用。
您可以尝试:
handler:function(){this.ownerCt.ownerCt.cancel.apply(this,arguments);}
,并删除scope:this
行。就像Lolo说的那样,this.cancel
在定义表单类时是未定义的
此问题的标准解决方案是在initComponent
内部创建项
数组:
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'); }
});
当调用
initComponent
时,会像预期的那样指向表单的实例。在您的代码中,此
指向不包含取消功能的全局窗口
对象。您也可以这样定义窗口按钮
...
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(){ ... }
...
。。。
initComponent:function(){
此按钮=[
{文本:'Close',
处理程序:函数(){
this.up('window').close()//