Last active
December 17, 2015 01:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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