javascript - 无法使用 apply() 调用 Backbone trigger() 方法

标签 javascript backbone.js marionette backbone-events

我正在尝试创建一个通用的辅助函数,该函数可用于触发 Backbone Marionette 模块上的事件。您可以向它传递一个带有“模块”、“事件”和“参数(可选)”键的对象,或此类对象的数组。如果您向它传递具有 moduleevent 属性的单个对象,它将为该模块调用该事件。如果您还向它传递一个 params 属性,它是一个数组,它也会传递它。这是我目前所拥有的:

/**
 * Triggers events for given module(s)
 *
 * @param {object|array} options - Either a single object with 'module', 'event' and 'params (optional)' keys, or an array of such objects
 */
var triggerModuleEvents = function (options) {
    require(['webapp'], function (WebApp) {
        var i, module, event, params;

        if (options instanceof Array) {
            for (i = options.length; i--;) {
                module = WebApp.module(options[i].module);
                event = options[i].event;
                params = undefined;
                params = options[i].params;

                if (typeof params === 'undefined') {
                    module.trigger(event);
                } else if (params instanceof Array) {
                    params.unshift(event);
                    module.trigger.apply(this, params);
                } else {
                    throw new TypeError('Params must be an array of parameters to pass with the event.');
                }
            }
        } else if (typeof options === 'object') {
            module = WebApp.module(options.module);
            event = options.event;
            module.trigger(event);
            params = undefined;
            params = options[i].params;

            if (typeof params === 'undefined') {
                module.trigger(event);
            } else if (params instanceof Array) {
                params.unshift(event);
                module.trigger.apply(this, params);
            } else {
                throw new TypeError('Params must be an array of parameters to pass with the event.');
            }
        } else {
            throw new TypeError("Argument must be an object with 'module' and 'event' keys, or an array of such objects.");
        }
    });
}

所以,我遇到的问题是触发方法 (module.trigger(event)) 工作正常,但我无法传递 params 数组。因为我不知道可能有多少个参数,所以我需要使用 apply() 但是有一些语法问题或者我遗漏了一些东西,只是看不到。具体来说,这一行似乎没有做任何事情 - module.trigger.apply(this, params) 下面是一个如何调用 helper 的示例:

Utils.triggerModuleEvents([
    {
        module: 'Foo',
        event: 'some:event:namespace',
        params: [ null, true ]
    },
    {
        module: 'Bar',
        event: 'another:event:namespace'
    }
]);

最佳答案

问题是使用 this 关键字,正如我所看到的,您只想更改参数顺序而不是将作用域上下文更改为 this,因此您应该将其更改为:

module.trigger.apply(module, params);

关于javascript - 无法使用 apply() 调用 Backbone trigger() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21687334/

相关文章:

javascript - 主干中的触发器和事件有什么区别?

Javascript 引用复制问题

javascript - 选择 <script>...</script> 的内容作为 html 正文中的纯文本?

javascript - item 对象的 Backbone where 方法

backbone.js - 是否应该在没有 Backbone Collection 的情况下使用 Marionette CollectionView?

javascript - Backbone : Remove hash from front in URL

javascript - 数据表:通过 DOM 数据源中的名称引用列

javascript - 键盘控制的网站

javascript - jQuery Mobile 拆分按钮 clickEvent 无 ID

javascript - 如何将 <thead> 部分添加到 tagName : 'table' ? 的 marionette collectionview 中