简体   繁体   English

如何从另一个控制器(而不是从模板)访问另一个控制器的属性

[英]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.

相关问题 如何从AngularJs中的另一个控制器访问控制器 - How to access a controller from another controller in AngularJs 来自AngularJS中另一个类的访问控制器属性 - Access controller property from another Class in AngularJS 如何从一个模板的控制器中检索ID,并在另一个模板的控制器中使用它? - How can I retrieve an id from one template's controller and use it in another template's controller? 如何通过范围从另一个控制器内部访问一个控制器的功能? - How to access functions of a controller from within another controller via scope? 如何从另一个控制器访问require'd控制器? - How to access a require'd controller from another controller? 如何从Angular中另一个控制器内部的控制器访问对象 - How to access object from a controller inside another controller in Angular 从另一个控制器触发一个控制器的“动作” - Triggering a controller's “actions” from another controller 如何从另一个控制器调用 Loopback4 控制器的方法 - How to call Loopback4 controller's method from another controller 如何在 MVC 中使用来自另一个 controller 的 controller - How to use a controller from another controller in MVC AngularJS,如何从另一个控制器调用控制器 - Angularjs, how call controller from another controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM