When should you use controllers versus routes in EmberJS?

I know this question may seem a bit duplicate, but another version of this question is outdated and some of the content (like Views) is not even part of ember.

I have about 4 weeks internship as a third party developer working with EmberJS. I still don't understand when is it better to use a route above the controller or vice versa. It seems to me that every action in the route can also be used in the controller.

I recently heard that ember routes need to be stateless where, since controllers need to be stateless .

What is the current state of the controllers and routes. When should you use each other?

+3


source to share


1 answer


Consider the following example to understand the state of a controller (or route or whatever) in simple terms and in the current context - let's say you have a page (like a form) with three tabs; each tab can be thought of as a state - it will invoke different components based on the state (or the tab you are in). Now, if for some reason you manage to go back and click the form link again, you will see that the state remains the same. (if you were on tab 2 when you typed back, then when you return to the form, you will still be on tab 2).

Thus, to maintain these states, controllers are the way to go, since they are single. The route would lose this information and start fresh. So basically your variables / objects in the controller will define the "state".

Route actions can be easily used as controller actions - see https://github.com/DockYard/ember-route-action-helper . So if your template for this route just uses the model as an object directly and you don't need to maintain "state", you can pretty much do without your controller.



But if your template used variables that needed manipulation, then you need a controller.

Hope this helps!

+4


source







All Articles