28

Is there any foolproof way to access controller from a route?

<a href="#" class="btn" {{action "someAction" user}}>add</a>

App.ApplicationRoute = Ember.Route.extend
  events:
    someAction: (user) ->
      console.log 'give me name from currentUser controller'

The someAction is very general and I think that ApplicationRoute is the best place for it.

aguadoe
  • 150
  • 2
  • 9
wryrych
  • 1,765
  • 4
  • 20
  • 31

2 Answers2

30

I think the method controllerFor should be available in this event:

App.ApplicationRoute = Ember.Route.extend
  events:
    someAction: (user) ->
      console.log this.controllerFor("currentUser").get("name")

Update in response to the questions in the comments:

It all depends on what you want to do. Worrying about DRY on such a basic method, does not make much sense imho.

In your kudos left case I would do this:

App.ApplicationRoute = Ember.Route.extend
  events:
    someAction: (user) ->
      this.controllerFor("currentUser").decrementKudos();
      // implement the decrementKudos in your controller

But I guess storing this one controller should also work, if this is too much code for you:

App.ApplicationRoute = Ember.Route.extend
  currentUserCon : this.controllerFor("currentUser")
  events:
    someAction: (user) ->
      this.currentUserCon.decrementKudos();
      // implement the decrementKudos in your controller
splattne
  • 102,760
  • 52
  • 202
  • 249
mavilein
  • 11,648
  • 4
  • 43
  • 48
  • Thanks, but is it possible to get a reference to that property, e.g can I store it in a variable so that I can operate on it? `name = this.controllerFor('currentUser').get 'name'` `return "hello, #{name}` is it possible? Accessing controllers in such a way many times is not DRY :) – wryrych Apr 10 '13 at 06:15
  • For example this would be an overkill: `this.controllerFor('currentUser').set 'kudosLeft', this.controllerFor('currentUser').get 'kudosLeft' - 1` :) – wryrych Apr 10 '13 at 06:17
  • @WojtekRyrych Also, this: http://emberjs.com/guides/cookbook/working_with_objects/incrementing_or_decrementing_a_property/ – Sam Selikoff Apr 20 '14 at 10:43
  • I'm not sure when this happened, but in Ember 1.12.0, `controllerFor` is deprecated. – blong Jun 10 '15 at 20:00
  • You can inject it with Ember.inject.controller() or from a route there is a this.controller property to use, – Epirocks Mar 21 '19 at 13:50
10

In the newer version of ember you can access current route's controller in route as below

Ember version 2.5

currentUserCon : this.controller

if you want to access other routes controller then use controllerFor method.

this.controllerFor('path to controller'); //put path to controller as parameter.
murli2308
  • 2,976
  • 4
  • 26
  • 47
  • 3
    I've found (at least in my Ember 2.14 app) that `this.controller` is `undefined` at least through the `activate()` route hook, but `controllerFor('current.route.name')` will return the controller. – Tom Wayson Sep 05 '17 at 21:06
  • Like @TomWayson I have found that this doesn't work in Ember 2.13 . The doco suggests it's there but if fails when you try to use it. . I was able to use `this.controllerFor` successfully. – glaucon Mar 13 '18 at 00:38