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
. The route definition is passed on to Backbone's standard routing handlers. This means that you define routes like you normally would. However, instead of providing a callback method that exists on the router, you provide a callback method that exists on the controller, which you specify for the router instance (see below.)
MyRouter = Backbone.Marionette.AppRouter.extend({
// "someMethod" must exist at controller.someMethod
appRoutes: {
"some/route": "someMethod"
},
/* standard routes can be mixed with appRoutes/Controllers above */
routes : {
"some/otherRoute" : "someOtherMethod"
},
someOtherMethod : function(){
// do something here.
}
});
You can also add standard routes to an AppRouter with methods on the router.
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 constructor:
myObj = {
someMethod: function(){ /*...*/ }
};
new MyRouter({
controller: myObj
});
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.