Skip to content

Instantly share code, notes, and snippets.

@vermilion1
Last active December 17, 2015 01:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vermilion1/5525972 to your computer and use it in GitHub Desktop.
Save vermilion1/5525972 to your computer and use it in GitHub Desktop.
Override Marionette route to have ability to pass different controllers to the single router
Marionette.AppRouter.prototype.processAppRoutes = function(controller, appRoutes) {
var routeNames = _.keys(appRoutes).reverse(); // Backbone requires reverted order of routes
_.each(routeNames, function(route) {
var methodName = appRoutes[route];
var params = methodName.split('#');
var ctrl = controller;
var method;
if (params.length > 1) {
var ctrlName = params[0];
ctrl = this[ctrlName] || this.options[ctrlName];
methodName = params[1];
if (!ctrl || !methodName) {
throw new Error('Please specify correct route');
}
}
method = ctrl[methodName];
if (!method) {
throw new Error('Method "' + methodName + '" was not found in the controller');
}
this.route(route, function () {
this.trigger('before:route:change');
method.apply(ctrl, arguments);
});
}, this);
};
appRoutes: {
'search': 'search#search'
'*any' : 'common#notFound'
},
initialize: function () {
this.common = new Common();
this.search = new Search();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment