Ember.js (RC1) - Getting an Instance of Controllers and Views

Within Ember, it's not entirely obvious how to get access to an instance of a controller or view object. This is something that I found really frustrating when trying to test my Javascript.

After hanging out on the #emberjs IRC channel for awhile, a friendly user pointed me to the following code snippet.


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

// view:NameOfView
var myView = window.App.__container__.lookup('view:Post');

What Am I Looking For?

If you find yourself having trouble figuring out the string you need to lookup, use the following code to return a dictionary of all objects used in your app. From there, you can easily pop open each object in the console and look for the _debugContainerKey, which is used when looking up the instance of the class.


// Returns all the classes used in your app, look for the _debugContainerKey
var classes = window.App.__container__.cache.dict

// Grab the object
var instantiatedClass = window.App.__container__.lookup('router:main');