2013-09-11 2 views
0

에 목록에서 하나 개의 항목을 렌더링, 나는 담론의 공동 설립자의 블로그 게시물에서 복사 아약스 호출을 사용하여 모든 레스토랑의 목록을 http://eviltrout.com/2013/02/27/adding-to-discourse-part-1.html내 엠버 응용 프로그램에서 엠버

App.Restaurant.reopenClass({ 

    findAll: function() { 

    return $.getJSON("restaurants").then(
     function(response) { 
     var links = Em.A(); 
     response.restaurants.map(function (attrs) { 
      links.pushObject(App.Restaurant.create(attrs)); 
     }); 
     return links; 
     } 
    ); 
    }, 

나는 레스토랑 경로가 설정되어 이는 최대 findall은 위에 표시된 호출하고 레스토랑은 각 레스토랑에 대한 링크가이 같은 restaurants 템플릿으로 표시되는 응용 프로그램 템플릿

App.RestaurantsRoute = Ember.Route.extend({ 
model: function(params) { 
    return(App.Restaurant.findAll(params)); 

    }, 

    renderTemplate: function() { 

    this.render('restaurants', {into: 'application'}); 
    } 

}); 

로 렌더링합니다. 나는 또한 엠버 라우터에서 restaurant 템플릿

<script type="text/x-handlebars" id="restaurants"> 

    <div class='span4'> 
     {{#each item in model}} 
     <li> {{#link-to 'restaurant' item}} 
     {{ item.name }} 
     {{/link-to }}</li> 
    {{/each}} 

     </ul> 
    </div> 

    <div class="span4 offset4"> 
    {{ outlet}} 
    </div> 

</script> 

을 포함 시켰습니다, 그래서 저는이

this.resource("restaurants", function(){ 
     this.resource("restaurant", { path: ':restaurant_id'}); 

    }); 

처럼 설정 상위/하위 경로, 나는 내가 클릭하면 바라고 있습니다 레스토랑 목록에서 특정 레스토랑 링크, 그것은 restaurantS (복수)에 정의 된 outlet 템플릿에이 restaurant 템플릿을 삽입 할 수 있습니다

<script type="text/x-handlebars" id="restaurant"> 
     this text is getting rendered 
     {{ item }} //item nor item.name are getting rendered   
</script> 

이 레스토랑 템플릿은 렌더링되지만 item의 데이터는 표시되지 않습니다.

목록에서 {{#link-to 'restaurant' item}}을 누르면 item은 해당 레스토랑을 나타냅니다.

이 설정에서 Ember는 findAll 호출에서 이미로드 되었더라도 특정 항목을 검색하기 위해 다른 Ajax 호출을해야합니까?

내가 (다시) 개별 식당 쿼리해야 할 경우에

나는 개인 레스토랑

App.RestaurantRoute = Ember.Route.extend({ 
    model: function(params) { 
    console.log(params); 
    console.log('resto'); 
    return App.Restaurant.findItem(params); 
    } 

}); 

과 레스토랑 모델

App.Restaurant.reopenClass({ 

findItem: function(){ 
    console.log('is this getting called? No...'); 
    return 'blah' 
    } 

에있는 FindItem에 대한 새 경로를 생성 그러나 그 console.logs의 어떤 것도 호출되지 않습니다.

엠버 시동기 비디오 https://www.youtube.com/watch?v=1QHrlFlaXdI에서 Tom Dale이 목록에서 블로그 게시물을 클릭하면 해당 게시물은 내가했던 것처럼 경로를 설정하는 것 외에는 아무것도 할 필요없이 정의 된 템플릿에 나타납니다. 게시물을 수신 할 게시물 템플릿 내의 {{outlet}}.

이 상황에서 왜 저와 같은 것이 효과가 없는지 알 수 있습니까?

답변

0

restaurant 경로로 이동하면 item은이 경로에 model이됩니다.템플릿에 따라서

, 당신은 당신은

또한 모델 후크가 호출되지 않으며, 더 console.log의이 레스토랑의 이름을 볼 수 있습니다

<script type="text/x-handlebars" id="restaurant"> 
    this text is getting rendered 
    {{ model.name }}  
    </script> 

을 시도하는 경우 작동하지 않는,

주 이유는 동적 세그먼트가있는 경로 만이 URL을 통해 입력 될 때 호출의 모델 훅을해야합니다. 전환을 통해 경로가 입력되면 (예 : 링크 - 핸들러 도우미 사용) 모델 컨텍스트가 이미 제공되고 훅이 실행되지 않습니다. 동적 세그먼트가없는 경로는 항상 모델 후크를 실행합니다.

이제 모든 것이 명확 해집니다.

+0

고마워요. Ember가 어떤 종류의 오류 메시지도 표시하지 않았으므로 그 문제가 무엇인지 전혀 알지 못했습니다. 왜 템플릿에 '항목'이 없다고 말하지 않을지 궁금합니다. – Leahcim