具有backbone.js的多个路由器

Ind*_*ial 14 router backbone.js

我可以使用多个路由器backbone.js,这些路由器不会相互干扰,没有任何问题吗?还是有什么我应该关注的?

代码示例:

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();
Run Code Online (Sandbox Code Playgroud)

tax*_*ian 8

是的,它运作得很好; 你遇到问题的唯一一次是他们是否有相互冲突的路线.有一种解决方法可以使它以这种方式工作,但它有点像黑客.

只要您避免让多个路由器尝试处理相同的路由,您应该没问题.