Code coverage report for src/marionette.approuter.js

Statements: 100% (13 / 13)      Branches: 100% (4 / 4)      Functions: 100% (3 / 3)      Lines: 100% (13 / 13)     

All files » src/ » marionette.approuter.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                    1     11   11   11 10 10               10   10 14 14   14 1     13          
// 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 pieces 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.apply(this, slice(arguments));
 
    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 routeNames = _.keys(appRoutes).reverse(); // Backbone requires reverted order of routes
 
    _.each(routeNames, function(route) {
      var methodName = appRoutes[route];
      var method = controller[methodName];
 
      if (!method) {
        throw new Error("Method '" + methodName + "' was not found on the controller");
      }
 
      this.route(route, methodName, _.bind(method, controller));
    }, this);
  }
});