In general, you only need a single Router
to power your entire site. However, if you have reason to do so, you can easily include multiple Routers
, and Backbone will happily allow this. If two or more Routers
on the same page match a particular route, Backbone will trigger the route from the first Router
with a matching route defined.
There are two main reasons why you should use multiple Routers
. The first reason is to separate your routing into logical groups. For instance, in an earlier example, we used a conditional to add certain admin-only routes to the Router
class when the current user was an administrator. If our site has enough of these routes, it might make sense to create a separate Router
for the admin-only routes, as follows:
var NormalRouter = Backbone.Router({ routes: { // routes for all users would go here } }; var AdminRouter = Backbone.Router({ routes: { // routes for admin users only would go here } }; new NormalRouter...