< >

Marionette.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 Routes

Configure an AppRouter with appRoutes. The route definition is passed on to Backbone's standard routing handlers. This means that you define routes like you normally would. Instead of providing a callback method that exists on the router, though, you provide a callback method that exists on the controller that you specify for the router instance (see below).

MyRouter = Backbone.Marionette.AppRouter.extend({
  appRoutes: {
    "some/route": "someMethod"
  }
});

You can also add standard routes to an AppRouter, with methods on the router.

Specify A Controller

App routers can only use one controller object. You can either specify this directly in the router definition:

someController = {
  someMethod: function(){ /*...*/ }
};

Backbone.Marionette.AppRouter.extend({
  controller: someController
});

Or in a parameter to the contructor:

myObj = {
  someMethod: function(){ /*...*/ }
};

new MyRouter({
  controller: myObj
});

Or

The object that is used as the controller has no requirements, other than it will contain the methods that you specified in the appRoutes.

It is recommended that you divide your controller objects into smaller pieces of related functionality and have multiple routers / controllers, instead of just one giant router and controller.

Improve this page