如何在单个按钮单击事件上运行两个函数

时间:2013-03-15 04:08:33

标签: javascript extjs extjs4

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

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时,我无法同时运行runFirstrunSecond

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

请您告诉我如何在单击按钮上运行两个功能?

1 个答案:

答案 0 :(得分: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);
            }
        }
    });
}