Question

I'm trying to create a generic helper function which can be used to trigger events on Backbone Marionette modules. You can pass it an object with 'module', 'event' and 'params (optional)' keys, or an array of such objects. If you pass it a single object with module and event properties, it will call that event for that module. If you also pass it a params property which is an array, it will pass that along as well. This is what I have so far:

/**
 * 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.");
        }
    });
}

So, the problem I'm having is that the trigger method (module.trigger(event)) works just fine but I'm unable to pass along the params array. Since I have no idea how many parameters there may be, I need to use apply() but there's some syntax issue or something I'm missing and just can't see. Specifically, this line doesn't seem to do anything - module.trigger.apply(this, params) Here is an example of how the helper should be called:

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

Solution

the problem is using this keyword, as I see all you want to do is just change the arguments order and not changing the scope context to this, so you should change it to:

module.trigger.apply(module, params);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top