UNPKG

lettuce

Version:

Lettuce JS, Mini Mobile Framework for Romantic with DSL.

67 lines (56 loc) 1.9 kB
// 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){ Backbone.Router.prototype.constructor.call(this, options); if (this.appRoutes){ var controller = this.controller; if (options && options.controller) { controller = options.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); } } });