Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access controller from route in Ember?

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.

like image 929
wryrych Avatar asked Apr 09 '13 20:04

wryrych


People also ask

What is Controller in Ember?

What is a Controller? A Controller is routable object which receives a single property from the Route – model – which is the return value of the Route's model() method. The model is passed from the Route to the Controller by default using the setupController() function.

What is route in Ember JS?

An Ember route is built with three parts: An entry in the Ember router ( /app/router. js ), which maps between our route name and a specific URI. A route handler file, which sets up what should happen when that route is loaded (app/routes/about.

What is the prime task performed by controllers in Ember JS?

What are the prime tasks that are performed by controllers in Ember. js? Decorating the model which is returned by the route is a very essential task that needs to be performed in Ember.

How do I use Ember data?

Ember Data is also designed to work with streaming servers, like those powered by WebSockets. You can open a socket to your server and push changes into Ember Data whenever they occur, giving your app a real-time user interface that is always up-to-date.


1 Answers

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 
like image 67
mavilein Avatar answered Sep 22 '22 18:09

mavilein