Passing models to controller via `modelFor` and `setupController` or `inject`?


#1

Our app has a mix of passing models to controllers both in routes via modelFor and setupController and in the controller itself using inject. I can’t figure out if they’re equivalent or not, and when it would make sense to use one or the other?


#2

I get how you would pass the model to the controller via modelFor but not sure about how you would in setupController. Inject I’m guess you mean you reach for an ancestor controller and look at its model.

I would stick with modelFor if you’re literally just threading a model through. Example:

model() {
  return this.modelFor('parent');
}

An example of when I’d opt for using inject is when I’m dealing with a hash where I am threading an ancestor model but also have a model I need to back the controller as well.

model() {
  // I personally wouldn't do this, and opt for using Inject instead
  return Ember.RSVP.hash({
    parent: this.modelFor('parent'),
    actualModel: this.store.find('...')
  });
}

#3

We do both. The idea is we have multiple models we need in the controller. So we do something like this:

model: function() {
  return this.modelFor('apps/app/billing');
},

setupController: function(controller, model) {
  controller.setProperties({
    model: model,
    app: this.modelFor('apps/app'),
    admin: this.modelFor('apps')
  });
}