【发布时间】:2013-10-08 17:00:03
【问题描述】:
我是 extjs 的新手,我正在使用 MVC 架构。
当我的应用程序引用控制器的方法时,我会这样做(MyApp.Application):
Mb.app.getController('Main').myMethod();
它已经很长了,但我认为这是这样做的。
当控制器在闭包中调用它自己的方法时,我被引导使用这段代码(在MyApp.controller.Main:
controllerMethodOne: function(){
Ext.Ajax.request({
url: ...,
params: ...,
success: (function(response){
list = Ext.JSON.decode(response.responseText);
list.forEach(function(item){
storeMenu.add(
Ext.create('Ext.menu.Item', {
text: item.text,
handler: function(el){MyApp.app.getController('Main').controllerMethodTwo()}
})
)
})
})
})
},
我用MyApp.app.getController('Main').controllerMethodTwo() 引用了该方法,因为this 没有引用闭包中的控制器对象,因此this..controllerMethodTwo() 不起作用。
我觉得这完全令人费解,我希望有人有一个想法来解决这个MyApp.app.getController-workaround。
更新
感谢所有可以优化我的代码并提出的建议:
// in my controller
mixins: ['Mb.controller.mixin.StoreMenu'],
// I use that style of menus in two controllers thats why I use a mixin
init: function() {
this.control({
'#vg_storeMenu menuitem': {
click: this.onStoreMenuClicked
}
})
},
// the controller mixin
Ext.define('Mb.controller.mixin.StoreMenu', {
extend: 'Ext.app.Controller',
buildStoreMenu: function(store_name){
var storeMenu = Ext.ComponentQuery.query('#' + store_name + 'Menu')[0];
Ext.Ajax.request({
url: Paths.ajax + 'json.php',
params: {list: store_name + 's'},
success: (function(response){
list = Ext.JSON.decode(response.responseText);
items = Ext.Array.map(list, function(item) {
return {
xtype: 'menuitem',
text: item.text
}
});
storeMenu.add(items);
})
})
},
onStoreMenuClicked: function(el){
...
}
});
【问题讨论】:
标签: extjs controller closures extjs4.2