2013-05-30 1 views
2

내 응용 프로그램에는 LoginController 및 RsvpController라는 두 개의 컨트롤러가 있습니다. 현재 로그인과 관련된 Rsvps 목록을로드하려고합니다.다른 컨트롤러 (템플릿이 아닌)에서 다른 컨트롤러의 속성에 액세스하는 방법

내 로그인 코드가 작동하는 것 같다 - 그러나 나는 다음과 같은 경고를 방지하는 데 문제 : 가 :

가 나는 또한 난 의심 "중단 컨트롤러 # controllerFor이되지 않습니다, 컨트롤러 # 대신해야 사용하십시오" ember 방법을 사용하지 않으므로 더 많은 ember 기능을 사용하려면이 코드를 향상시키는 방법에 대한 의견을 보내 주시면 감사하겠습니다. 거의, 템플릿 당신이 옳은 일을하고있는 당신은, 사용 중단 경고가 나오는

<script type="text/x-handlebars" id="rsvp"> 
    <p>placeholder for <b>RSVP</b></p> 

    {{#if controllers.login.name }} 
     <!-- rsvp code here --> 
     Logged in with code {{controllers.login.code}} 

     {{ loadModel }} <!-- call loadModel function to populate model when this is rendered --> 

     {{#each model}} 
      <p>givenName {{givenname}}</p> 
     {{/each}} 

    {{else}} 
     <i>Please login first!</i> 
    {{/if}} 

</script> 

답변

8

:

여기
App.RsvpController = Ember.ArrayController.extend({ 
    needs: ['login'], // borrows from login controller 
    content: [], 

    loadModel : function() { 
     var theCode = this.controllerFor('login').get('code'); 
     console.log("Loading Model for code " + theCode); 

     var rsvpArray = App.Rsvp.find({ 
      code : theCode 
     }); 
     this.set('content', rsvpArray); 

     rsvpArray.addObserver('isLoaded', function() { 
      console.log("Loaded rsvpArray" + Em.inspect(this)); 
     }); 


    }.property('controllers.login.code'),  // reload model if the user changes login 
}); 

가 index.html을의 일부입니다 : 여기

는 컨트롤러 controllerFor으로 전화하여 this.get('controllers.login')과 같은 로그인 컨트롤러를 피할 수 있습니다. 코드는 다음과 같이 수정됩니다.

App.RsvpController = Ember.ArrayController.extend({ 
    needs: ['login'], // borrows from login controller 
    content: [], 

    loadModel : function() { 
    var theCode = this.get('controllers.login.code'); // here the change 
    console.log("Loading Model for code " + theCode); 

    var rsvpArray = App.Rsvp.find({ 
     code : theCode 
    }); 
    this.set('content', rsvpArray); 

    rsvpArray.addObserver('isLoaded', function() { 
     console.log("Loaded rsvpArray" + Em.inspect(this)); 
    }); 

    }.property('controllers.login.code'), // reload model if the user changes login 
}); 

희망 하시겠습니까?

+0

그래, 고쳐 줘. 나는 그것을 시도했다고 확신한다. (하지만 그 구문을 시도했을 때 아마도 다른 것이 깨져있을 것이다.) – user2438445

관련 문제