[英]How can I change the scope using control method of a Controller in ExtJS 4?
In ExtJS with control() method of Ext.app.Controller class I can add listeners to components selected via Ext.ComponentQuery inside controller's init() method. 在ExtJS中使用Ext.app.Controller类的control()方法,我可以将侦听器添加到通过控制器的init()方法内的Ext.ComponentQuery选择的组件。
Doing so by default I have the controller scope inside my handler functions. 默认情况下这样做我的处理程序函数中有控制器范围。 Is there a way to specify some other scope here?
有没有办法在这里指定其他范围?
For example in this case: 例如,在这种情况下:
Ext.define('BackOffice.controller.BaseList', {
extend: 'Ext.app.Controller',
refs: [
// some refs
],
init: function() {
this.control({
'bo-list-view > toolbar > button[action=create-new]': {
click: this.onCreateNewClick
},
'bo-list-view > toolbar > button[action=edit]': {
click: this.onEditClick
},
'bo-list-view > toolbar > button[action=refresh]': {
click: this.onRefreshClick
}
})
},
// ...
});
In handler functions I want to know the exact buttons clicked to perform some actions based on this information. 在处理程序函数中,我想知道单击的确切按钮,以根据此信息执行某些操作。
I've tried specifying scope within button declaration via scope field, this doesn't help. 我已经尝试通过范围字段在按钮声明中指定范围,这没有帮助。
Also I wanted to use Ext.bind() to solve the issue, but to bind the function to I need some reference to the button again. 另外我想使用Ext.bind()来解决问题,但要将函数绑定到我需要再次引用该按钮。
Using .on() to add listeners doesn't work in init() because it's called before application's launch method. 使用.on()添加侦听器在init()中不起作用,因为它在应用程序的启动方法之前被调用。
Is there any way to add listeners to this buttons from controller saving the scope? 有没有办法从控制器添加监听器来保存范围?
Update 更新
The reference to the button is passed as the first argument into the handler function. 对按钮的引用作为第一个参数传递给处理函数。 This solves my problem, but still looking for a way to define the scope inside control() method.
这解决了我的问题,但仍在寻找一种在control()方法中定义范围的方法。
You can achieve more advanced scope functionality by using a configuration object. 您可以使用配置对象实现更高级的范围功能。
Ext.define('BackOffice.controller.BaseList', {
extend: 'Ext.app.Controller',
refs: [
// some refs
],
init: function() {
this.control({
'bo-list-view > toolbar > button[action=create-new]': {
// This will call onCreateNewClick with global window as the scope
click: {fn: this.onCreateNewClick, scope: window}
},
'bo-list-view > toolbar > button[action=edit]': {
click: {fn: this.onEditClick, scope: otherScopeObj}
},
'bo-list-view > toolbar > button[action=refresh]': {
click: this.onRefreshClick
}
});
},
// ...
});
I don't know if all the normal configs work (ie single: true) but scope does. 我不知道所有正常的配置是否有效(即单一:真)但范围确实如此。
如果你看一下click
事件的描述: http : //docs.sencha.com/ext-js/4-0/# ! /api/Ext.button.Button-event-click你会看到第一个参数将是是指向按钮的指针,即您不需要更改范围 - 只需在函数中声明参数,您就会知道按下了哪个按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.