11

I would like to init a list of all users in ApplicationController and then show them in dropdown in another view. How can I get access to the ApplicationController from different views?

Here is relevant code:

  App.ApplicationRoute = Ember.Route.extend({
   setupController:function(controller) {
    controller.set('users', App.User.find());
    controller.set('selectedUser', null);
  }
 });

 <script type="text/x-handlebars" data-template-name="users">
   {{view Ember.Select
   contentBinding="App.ApplicationController.users"
   optionValuePath="content.id"
   optionLabelPath="content.fullName"
   selectionBinding="App.ApplicationControllerselectedUser"}}

   selected user: {{App.ApplicationController.selectedUser.fullName}}
 </script>

2 Answers 2

19

Specify needs in your view's controller

App.UsersController = Ember.Controller.extend({
  needs: ['application']
});

In your view you can then access the application controller as follows

controllers.application

In your example

<script type="text/x-handlebars" data-template-name="users">
  {{view Ember.Select
         contentBinding="controllers.application.users"
         optionValuePath="content.id"
         optionLabelPath="content.fullName"
         selectionBinding="controllers.application.selectedUser"}}

  selected user: {{controllers.application.selectedUser.fullName}}
</script>
2
  • Crazy, that didn't show up when I used {{debugger}}, i.e. this.controllers returned null. But it works now! Dec 19, 2014 at 23:34
  • That is because this in context of the template is equivalent to controller. which can be proven by printing constructors, like: {{this.constructor}} // App.ApplicationController {{controller.constructor}} // App.ApplicationController:ember Feb 12, 2015 at 19:17
2

You can look for the controller via the container

this.container.lookup('controller:application')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.