[英]How to access another controller's property from another controller (not from a template)
My Application has two controllers, LoginController and RsvpController. 我的应用程序有两个控制器,LoginController和RsvpController。 I want to load a list of Rsvps which are relevant to the current login. 我想加载与当前登录相关的Rsvps列表。
My login code seems to work -- but I'm having trouble avoiding the following warning: "DEPRECATION: Controller#controllerFor is deprecated, please use Controller#needs instead" 我的登录代码似乎有效 - 但是我无法避免以下警告:“DEPRECATION:Controller#controllerFor已被弃用,请使用Controller#needs”
I also suspect I'm not doing things the ember way, so I'd appreciate any comments as to how to improve this code to use more ember functionality. 我也怀疑我不是在做烬的方式,所以我很感激如何改进这些代码以使用更多的ember功能。
Here is the controller: 这是控制器:
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
});
Here is part of index.html: 这是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>
You where almost there, in your template you are doing things right, the deprecation warning comes from the call to controllerFor
which you can avoid getting your login controller like so this.get('controllers.login')
, here your code modified: 你几乎就在那里,在你的模板中,你正在做的事情,弃用警告来自对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
});
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.