14

Can I use multiple routers in backbone.js, that don't interfere with each other route-wise, without any issues? Or is there something that I should be concerned about?

Code sample:

myapp.routers.main = Backbone.Router.extend({
    routes : { "": "index" },
    index : function() { console.log("routed by main router");}    
});

myapp.routers.another = Backbone.Router.extend({
    routes : { "notmain": "index" },
    index : function() { console.log("routed by another router");}    
});

mainrouter = new vaikava.routers.main;
notmainrouter = new vaikava.routers.another;
Backbone.history.start();
machineghost
  • 33,529
  • 30
  • 159
  • 234
Industrial
  • 41,400
  • 69
  • 194
  • 289
  • The easiest way to answer this question is to try it out and see if it works. As far as I know there should be no problem with what you've outlined. – nrabinowitz Jan 22 '12 at 00:02
  • I tried it myself - no errors, but as this is my first real backbone app and I've read that there could be issues with multiple routers, I wanted to ask here – Industrial Jan 22 '12 at 00:06

1 Answers1

8

Yes, it works just fine; the only time you'd have a problem is if they have conflicting routes. There is a workaround that makes it work that way as well, but it's a bit of a hack.

As long as you avoid having multiple routers trying to handle the same route you should be fine.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Hi Taxillian. Thanks a lot for your answer. Can you check out another related `backbone.js`-question for me? http://stackoverflow.com/questions/8957543/working-with-state-in-backbone-js-logging-in-a-user – Industrial Jan 22 '12 at 10:21