1

我正在使用 MVC Extjs,我希望在按钮的单击事件上运行两个不同的函数。到目前为止,这是我的控制器代码:

Ext.define('MyApp.controller.myController', {
    extend: 'Ext.app.Controller',

    runFirst: function(button, e, options) {
        console.log('first function is running');
    },

    runSecond: function(button, e, options) {
        console.log('second function is running');
    },


    init: function(application) {
        this.control({
            "#myButton": {
                click: this.runFirst, runSecond //THIS PART DOESN'T WORK :(
            }
        });
    }

});

当我单击 myButton 时,我无法同时runFirst运行。runSecond

您可以在此处下载所有代码:https ://github.com/nitzaalfinas/Extjs-run-2-function-with-one-click/tree/one

您能告诉我如何在单击一次按钮时运行两个功能吗?

4

1 回答 1

6

你在做什么不是有效的Javascript。您不能将两个不同的值分配给单个变量(就是这样click:

因此,您可以通过以下方式实现它:

init: function(application) {
    this.control({
        "#myButton": {
            click: this.runBoth
        }
    });
}

runBoth: function(button, e, options) {
    this.runFirst(button, e, options);
    this.runSecond(button, e, options);
}

或者,使用匿名函数:

init: function(application) {
    this.control({
        "#myButton": {
            click: function(button, e, options) {
                this.runFirst(button, e, options);
                this.runSecond(button, e, options);
            }
        }
    });
}
于 2013-03-15T04:18:19.197 回答