Plato on Github
Report Home
approuter.js
Maintainability
57.60
Estimated # Bugs
0.35
Difficulty
21.15
SLOC/LSLOC
47 / 31
Function weight
By Complexity
By SLOC
// AppRouter // --------- // Reduce the boilerplate code of handling route events // and then calling a single method on another object. // Have your routers configured to call the method on // your object, directly. // // Configure an AppRouter with `appRoutes`. // // App routers can only take one `controller` object. // It is recommended that you divide your controller // objects in to smaller peices of related functionality // and have multiple routers / controllers, instead of // just one giant router and controller. // // You can also add standard routes to an AppRouter. Marionette.AppRouter = Backbone.Router.extend({ constructor: function(options){ var args = Array.prototype.slice.apply(arguments); Backbone.Router.prototype.constructor.apply(this, args); this.options = options; if (this.appRoutes){ var controller = Marionette.getOption(this, "controller"); this.processAppRoutes(controller, this.appRoutes); } }, // Internal method to process the `appRoutes` for the // router, and turn them in to routes that trigger the // specified method on the specified `controller`. processAppRoutes: function(controller, appRoutes){ var method, methodName; var route, routesLength, i; var routes = []; var router = this; for(route in appRoutes){ if (appRoutes.hasOwnProperty(route)){ routes.unshift([route, appRoutes[route]]); } } routesLength = routes.length; for (i = 0; i < routesLength; i++){ route = routes[i][0]; methodName = routes[i][1]; method = controller[methodName]; if (!method){ var msg = "Method '" + methodName + "' was not found on the controller"; var err = new Error(msg); err.name = "NoMethodError"; throw err; } method = _.bind(method, controller); router.route(route, methodName, method); } } });