2013-05-30 62 views
2

我的應用程序有兩個控制器,LoginController和RsvpController。我想加載與當前登錄相關的Rsvps列表。如何從另一個控制器(不是來自模板)訪問另一個控制器的屬性

我登錄的代碼似乎工作 - 但我無法避免以下警告: 「棄用:控制器#controllerFor已過時,請使用控制器#改爲需要」

我也懷疑我沒有按照餘燼的方式來做事情,所以我很感激任何關於如何改進這段代碼以使用更多餘燼功能的評論。

這裏是控制器:

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的一部分:

<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

你那裏幾乎沒有,在你的模板你正在做正確的事情,棄用警告來自撥打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

相關問題