2013-12-10 39 views
0

我在這裏有複雜的路由。構建子視圖的URL以導航不同的父視圖。如何構建骨幹路由的動態url?

domain.com/categories/1/details 
domain.com/categories/1/price 
domain.com/categories/1/owner 

domain.com/categories/2/details 
domain.com/categories/2/price 
domain.com/categories/2/owner 

我需要構建詳細信息,價格和所有者視圖的網址。

<a href="#/categories/id/price">Price</a> 
<a href="#/categories/id/details">details</a> 
<a href="#/categories/id/owner">owner</a> 

需要動態替換ID!

我該如何構建它們?

+0

您是否嘗試過閱讀[Backbone.Router(http://backbonejs.org/#Router)文檔? – fbynite

+0

呀。但我需要瀏覽共同的意見「categories /:id /:section」我可以路由它,但問題是「categories /?/ section」。 – user1834809

回答

1

你可以試試這個:

var myRouter = Backbone.Router.extend({ 
      routes : {} 
    }); 
    myRouter.on('route:categories/:id/details' function() { 

    }); 

    myRouter.on('route:categories/:id/price' function() { 

    }); 

    myRouter.on('route:categories/:id/owner' function() { 

    }); 
+0

對不起,請閱讀該問題,編輯 – user1834809

0

在您的視圖的模板,你必須:

<script type="text/template" id="my-template"> 
    <a href="#/categories/<%= id %>/price">Price</a> 
    <a href="#/categories/<%= id %>/details">details</a> 
    <a href="#/categories/<%= id %>/owner">owner</a> 
</script> 

然後你的視圖的渲染方法中,你只需要在ID變量傳遞。

<script type="text/javascript"> 
    var userId = 42; 

    var MyView = Backbone.View.extend({ 
     initialize: function(){ 
      this.render(); 
     }, 
     render: function(){ 
      // Compile the template using underscore 
      var template = _.template($("#my-template").html(), {id: userId}); 

      // Load the compiled HTML into the Backbone "el" 
      this.$el.html(template); 
     } 
    }); 
</script> 

這裏是多一點關於這個問題,可能會幫助:http://backbonetutorials.com/what-is-a-view/

+0

我不想重新初始化總視圖。 – user1834809

+0

所以當視圖第一次初始化時你沒有這些值?如果是這樣的話,你可以添加一個函數到你的視圖中,這個函數可以插入到DOM中,抓取A元素,然後修改SRC(使用jQuery)。 – Bart