1

Possible Duplicate:
Accessing controllers from other controllers

In Ember pre2 and before with the old Router style, you could get other controllers from the router, so if I were in a controller called PeopleController I could do something like this

App.PeopleController = Ember.Controller.extend({
     some_computed_property: (function() {
          return this.get('target.otherController.property_i_want');
     }).property('target.otherController.property_i_want')
});

or from a debug console

> App.router.get('otherController.property_i_want')

Both of these worked. Pre4 / the new routing style seems to break this. How do I get this functionality with the new router and pre4?

Community
  • 1
  • 1
wmarbut
  • 4,595
  • 7
  • 42
  • 72

4 Answers4

3

I asked a similar question; you can declare dependencies in the current version.

Accessing controllers from other controllers

Community
  • 1
  • 1
Aaron Renoir
  • 4,283
  • 1
  • 39
  • 61
3

I had a horrible hack:

Em.Route.reopen({init:function(){
  window.App.currentRoute = this;
  this._super.apply(this,arguments);
}})

Which lets you do things like:

App.currentRoute.controllerFor('something');
App.currentRoute.target...

EDIT: Currently, ember supports defining "needs" for controllers, as well as exposes the container for lookups:

App.__container__.lookup("controller:application").get("someProperty")

App.ApplicationController = Em.Controller.extend({
    needs: ["authentication","notifications"],
    init: function(){
       this._super.apply(this,arguments)
       console.log(this.get("controllers.authentication"), this.get("controllers.notifications"))
    }
})
Nthalk
  • 3,774
  • 1
  • 25
  • 19
2

Try this.controllerFor('other').get('property_i_want')

See last part of http://emberjs.com/guides/routing/setting-up-a-controller/

dechov
  • 1,833
  • 1
  • 15
  • 18
2

To access the controller from the console, set debugger; in your code, refresh the browser, that will halt execution where you set the debugger statement, then you can access your controller within the scope you're in, using

this.controllerFor('abs');

This also very useful in debugging template, you can insert {{debugger;}} and that gives you access to the whole scope of the template in the console, try for example to find out what your controller or your view are.

ken
  • 3,745
  • 6
  • 34
  • 49