2013-03-23 55 views
2

想知道下面的場景中是否有任何模式。它看起來像需要某種類型的狀態機:在backbone.js中處理具有狀態機的url和路由

  • 兩個集合(比方說,汽車和鳥,只是讓這件事)
  • 您可以深入瞭解或者收集和查看詳細視圖
  • 每個集合都有一個與之相關的視圖,還有每個視圖的詳細信息
  • 想要防止在router.on方法中重複代碼並保持乾燥(也許只有一種方法可以查看汽車狀態並執行某些操作,和鳥類一樣嗎?)

狀態:汽車 - 鳥
網址:#

狀態:汽車細節 - 鳥
網址:#/汽車/ 1

狀態:汽車的細節 - 鳥詳細
url:#/ cars/1/birds/1

狀態:汽車 - 鳥細節
網址:#鳥/ 1

回答

1
var Router = Backbone.Router.extend({ 
routes: { 
    '*variables': 'drawViews' 
}, 

parse: function(variables) { 
    values = variables.split("/"); 
    return values; 
}, 

drawViews: function(variables) { 
    values = this.parse(variables); 
//and here you check the first value and see it its car or bird then you get  
//next value of the array wich will be the index and so on. 
} 

});