2012-01-21 97 views
14

我可以使用多個路由器在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(); 
+0

回答這個問題最簡單的方法就是試試看看它是否有效。據我所知,你所描述的內容應該沒有問題。 – nrabinowitz

+0

我自己嘗試過 - 沒有錯誤,但由於這是我第一個真正的主幹應用程序,我讀過可能存在多個路由器的問題,我想在這裏問 – Industrial

回答

8

是的,它工作得很好;唯一一次你遇到問題的是他們有衝突的路線。 There is a workaround,它也可以這樣工作,但這有些破綻。

只要你避免讓多個路由器試圖處理相同的路由,你應該沒問題。

+0

您好Taxillian。非常感謝您的回答。你能檢查出另一個相關的'backbone.js'問題嗎? http://stackoverflow.com/questions/8957543/working-with-state-in-backbone-js-logging-in-a-user – Industrial