Best practice: Updating model in an action


#1

As my Ember apps grow, I’ve found myself having trouble trying to decide where to put actions that call save/destroy on a model.

The basic guidance of “routes handle model loading” and “controllers handle template state” would suggest state changes should happen in controller actions and then bubble to route for saving if necessary.

Sometimes, though, I have an isolated controller/template coupling that is rendered in a few different templates, loaded by different routes. If I have to save that model, I either need to:

  1. Put the saving in the controller, thus breaking MVC responsibilities
  2. Repeat the action across routes and extract to a mixin

Has anyone arrived at what they consider a consistent best practice for these types of actions?


#2

Saving in the controller isn’t necessarily a bad thing. I usually put save/destroy in the route when I can but there are some instances where it fits better inside the controller.

Also, if there’s an action that happens across multiple routes, you can always put the action in the application route and let action bubbling do its thing so you’re not repeating yourself.


#3

Agree with this, I used to have the same question but after looking at @wycats “Tale of two MVC”, that changed.

It is fine to have this kind of actions in the controller, after all, updating domain objects is part of its responsibilities and you are not breaking MVCLCTMRA[1] responsibilities.

[1] Ember is not MVC https://news.ycombinator.com/item?id=7138319