I agree with every point in this blog post except for “Bad Ember developers return RSVP.hash
Taras Mankovski
71

Because returning a single domain object from the route is the Ember pattern - the very name of the hook implies it (_model_, not _modelAndEtc_). Using hash/all is an indicator of a bad domain model and usually stems from one of the following:

  • Not traversing the model graph. For example, returning { post: post, comments: comments } instead of just `post`, and then accessing post.comments.
  • Omitting relationships. Sometimes data isn’t directly related to the parent, but should be. For example, a user form has a <country-list-selector>, so you throw in RSVP.hash and store.findAll(‘country’). But in reality, this list of countries is part of the domain model — something like user.availableCountries. So it should be part of the graph, even if it’s not obvious from the way the models are set up in the backend.
  • Loading tangential data that’s not related to the primary purpose of the route. The role of the route is to set up the controller for presentation of the primary data. If you need to render a <tweet-list> that has little to do with the primary model, that component itself should probably bear the responsibility for loading its data, separate from the normal route/controller flow.

In short, if your route becomes so complex that you’re returning multiple domain objects, you should probably create a new domain object that manages the relationships between these objects, so that your main application code still follows the Ember pattern.

Note that this advice corresponds exactly to Sandi Metz’s rule that in Rails applications, “controllers can instantiate only one object.” See her discussions and podcasts for a deeper dive on the subject.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.