Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  62] [ 3]  / answers: 1 / hits: 26616  / 11 Years ago, tue, april 9, 2013, 12:00:00

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.


More From » ember.js

 Answers
2

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

[#79011] Monday, April 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eliza

Total Points: 732
Total Questions: 96
Total Answers: 86

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;