2013-12-10 1 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 

세부 정보, 가격 및 소유자보기에 대한 URL을 구성해야합니다.

<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) 문서를 읽어 봤어 : 여기

<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> 

은 도움이 될 주제에 좀 더입니까? – fbynite

+0

yah. 하지만 공통 뷰 "categories/: id/: section"을 탐색해야하지만 경로를 지정할 수는 있지만 문제는 href = "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 변수를 전달해야합니다. http://backbonetutorials.com/what-is-a-view/

+0

전체보기를 다시 초기화하고 싶지 않습니다. – user1834809

+0

그래서 뷰가 처음 초기화 될 때이 값들을 가지고 있지 않습니까? 이 경우 DOM에 삽입하고 A 요소를 잡고 jQuery를 사용하여 SRC를 수정하는 뷰에 함수를 추가하면됩니다. – Bart

관련 문제