Reputation: 471
I started wondering about this after I read a discussion on where to put event logic, in the controller or in the route. Where should you handle (certain) events? https://github.com/emberjs/ember.js/pull/1209
In general, what is the convention/rule for putting something in the controller or the route?
Upvotes: 2
Views: 92
Reputation: 2043
I like to think of it as wondering what context you will need the action in. The actions bubble down in the following way:
controller -> current route -> parent route -> application route.
So in general if you can only see your action being used in the context of the controller, then it probably belongs there. If the route deals with multiple controllers (like if you use "render" in your templates) then the action belongs on that route. If multiple routes could call an action, then the action belongs on the parent.
In practice this often looks like writing them on the controller, then moving them to routes when you realize it's necessary.
Some Ember developers like to also use the nature of the action to determine where things go (although I think this is a much fuzzier way to go about things).
For instance according to this school of thought:
If the action has to do with routing (maybe one that ends with a "transitionTo") then it belongs in the router. If it does not deal with routing (say just selecting an item as active) then it belongs in the controller.
The important thing is to pick one heuristic and just be consistent with it so that you / other developers will be able to find the actions.
Upvotes: 4