在extjs MVC架构中如何将参数传递给函数

1 extjs extjs4 extjs-mvc

我想将参数"aaa"传递给disableFlied函数,但下面的代码不起作用:

init: function() {

    this.control({
        '#speedCheck': {
            change :this.disableFlied("aaa")
        }
    });

},
disableFlied: function(cmpName){

    var a = Ext.getCmp(cmpName);
    a.setDisabled(!a.isDisabled());

}
Run Code Online (Sandbox Code Playgroud)

如何将"aaa"传递给disableFlied函数?

rix*_*ixo 5

你必须传递一个函数作为事件处理程序,在这里你传递调用disableField自己的结果(即没有任何东西,因为这个方法不返回任何东西).您需要做的是创建另一个传递所需参数的函数.在Ext中,您可以使用该Ext.bind功能执行此操作.

以下是您应该如何使用它:

this.control({
    '#speedCheck': {
        change: Ext.bind(this.disableField, this, ['aaa'])
    }
});
Run Code Online (Sandbox Code Playgroud)

这将在disableField方法周围创建一个闭包.这相当于这个vanilla javascript代码:

    '#speedCheck': {
        // This is the default scope, but I prefer to make it
        // explicit that the handler must be called in the
        // current scope
        scope: this
        ,change: function() {
            // When this function is called, it will call
            // disableField with the desired parameter
            this.disableField('aaa');
        }
    }
Run Code Online (Sandbox Code Playgroud)