Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting access to Ember ApplicationController from another view

Tags:

ember.js

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>
like image 747
Misha Avatar asked Mar 27 '13 15:03

Misha


2 Answers

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>
like image 176
Thomas Avatar answered Oct 27 '22 23:10

Thomas


You can look for the controller via the container

this.container.lookup('controller:application')
like image 38
Alberto Avatar answered Oct 27 '22 22:10

Alberto