简体   繁体   English

如何使用ExtJS 4中Controller的控制方法更改范围?

[英]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.

相关问题 如何在extjs应用程序控制器中控制事件侦听器的范围 - How to control the scope of event listeners in the extjs app controller 控制器this.control()方法extjs 4 - Controller this.control() method extjs 4 如何从内部指令更改控制器的$ scope属性? - how can i change $scope property of controller from inside directive? Javascript-如何在没有“ this”的情况下更改范围和访问方法 - Javascript - How can I change scope and access method without 'this' Angularjs我可以从另一个控制器更改控制器的$ scope值吗? - Angularjs Can I change $scope value of a controller from another controller? 在使用Typescript时,如何从子控制器访问父控制器中的作用域函数? - How can I access a scope function in a parent controller from a child controller when using Typescript? AngularJS:如何根据控制器范围更改ng类三元运算符的值? - AngularJS:How can i change value of the ng-class ternary operator as per the controller scope? 如何在extjs4.1中的控制器中声明全局变量? - How can i declare global variable in the controller in extjs4.1? 如何在控制器中更改范围数据? - How to change scope data in controller? 我怎样才能正确界定这种“公共”方法的范围? - How can I scope this “public” method correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM