4
votes

In emberjs pre2 we could access controller or any method in controller from another controller in following way:

App.get('router').get('navController').method1();

Can anybody suggest what could be the similar code for emberjs rc1?

Thanks

3
I've been trying to follow Data down actions up paradigm. How you would do something similar is using the dependency injection @Def_Os suggested, but do it on the application route.David Lai

3 Answers

3
votes

Inside a Controller or a Route you can try

this.controllerFor("nav").method1()

Attention

This was correct answer when the question was asked but since controllerFor is deprecated, please check the answer by joscas

17
votes

Since controllerFor is deprecated, I think the a more correct way would be with needs:

this.get('controllers.nav').method1()

It requires declaring your needs in your controller:

App.YourController = Ember.ObjectController.extend({
  needs: ['nav'],
  ....
2
votes

In Ember 2, this works by injecting the controller you want access to:

export default Ember.Controller.extend({
  nav: Ember.inject.controller(),
});

Or, if you want to specify a name different than the controller name:

export default Ember.Controller.extend({
  navController: Ember.inject.controller('nav'),
});

You can then access the injected controller's methods like this:

this.get('navController').method1()