Accessing controllers from other controllers

后端 未结 1 1385
执念已碎
执念已碎 2020-12-05 15:10

I am building a project management app using ember.js-pre3 ember-data revision 11.

How do I initialize a couple of controllers and make them available globally. For

相关标签:
1条回答
  • 2020-12-05 15:47

    In the latest version of ember (ember-1.0.0-pre.3.js) you can do this by declaring controller dependencies. Once a dependency has been declared, it will be accessible via the controllers property. For example:

    window.App = Ember.Application.create();
    App.ApplicationController = Ember.Controller.extend({   
      needs: ['currentUser', 'users']
    });
    App.CurrentUserController = Ember.ObjectController.extend({
      content: 'mike'
    });
    App.UsersController = Ember.ArrayController.extend({
      content: ['mike', 'jen', 'sophia']
    });
    

    Since ApplicationController needs currentUser and users, those controllers are accessible via it's controllers property and can be used from within the application template:

    <script type="text/x-handlebars">
      <p>Signed in as {{controllers.currentUser.content}}</p>
      <h2>All Users:</h2>
      <ul>
        {{#each user in controllers.users}}
        <li> {{user}} </li>
        {{/each}}
      </ul>
    </script>
    

    Here's a working example: http://jsfiddle.net/mgrassotti/mPYEX/

    See https://github.com/emberjs/ember.js/blob/master/packages/ember-application/tests/system/controller_test.js for some examples

    0 讨论(0)
提交回复
热议问题