Search code examples
javascriptruby-on-railsmarionettebackbone-events

Override Backbone.Wreqr.radio handlers


At the company I work, we've decided to extract most of our Marionette.js code and split them up into several Rails Gems.

At this moment, I've managed to split up Marionette Controllers and Views. Some of the stuff inside Marionette Controllers are necessary for most of our eCommerce apps, so I extracted this JavaScript code into a few Gems. We'll then be able to use the stuff we've already split up into Gems and override / extend Controllers/Views when necessary.

For app-wide actions, we use Backbone.Wreqr. An example of an event handler found in one of our Gems:

Backbone.Wreqr.radio.commands.setHandler('product', 'show', function (slug) {
    API.showSingleProduct(slug);
    App.navigate("/products/" + slug);
});

However, for some of our apps, we'll need to override this handler to make it work with some custom stuff in our project.

Imagine I want to modify the above handler to something like this:

Backbone.Wreqr.radio.commands.setHandler('product', 'show', function (slug, color) {
    API.showSingleProduct(slug, color);
    App.navigate("/products/" + slug);
});

When I add such an event handler in our base project, the event will be called twice. I actually only need the last (customized) event handler to be fired.

Is there any way to override existing Backbone.Wreqr events? Or should I remove the previous handlers every time I want to customize the behaviour of a previous handler?


Solution

  • There's no need to remove the previous handler, setting a new one effectively overrides any handler assigned to the same namespace.

    Your code should work in terms of registering a single handler, the problem must be somewhere else.