How to get an instance of a controller in Ember.js?

I have a quick post about this exact subject on my Blog. It's a little big of a different method, but seems to work well for Ember.js RC1.

Check it out at: http://emersonlackey.com/article/emberjs-instance-of-controller-and-views

The basic idea is to do something like:

var myController = window.App.__container__.lookup('controller:Posts');

This answer works with RC1/RC2.

Now you can use the needs declaration in order to make the desired controller accessible. Here's an example:

Suppose I want to get something from my SettingsController from within my ApplicationController. I can do the following:

App.SettingsController = Ember.Controller.extend({
  isPublic: true
});

App.ApplicationController = Ember.Controller.extend({
  needs: 'settings',
  isPublicBinding: 'controllers.settings.isPublic'
});

Now in the context of my ApplicationController, I can just do this.get('isPublic')


You can access a controller instance inside an action in the router via router.get('invitesController'), see http://jsfiddle.net/pangratz666/Pk4k2/:

App.InvitesController = Ember.ArrayController.extend();

App.Router = Ember.Router.extend({
    root: Ember.Route.extend({
        route: '/',
        index: Ember.Route.extend({
            route: '/',
            connectOutlets: function(router, context) {
                var invitesController = router.get('invitesController');
            },
            anAction: function(router) {
                var invitesController = router.get('invitesController');
            }
        })
    })
});​