Why use setupController as opposed to model in a router?


#1

What is the difference between using setupController as opposed to setting the model property. I saw this in the Peepcode screencast and am trying to figure out why I would use each approach. Doesn’t this do the same thing as just setting model: food?

// from the peepcode screencast (excellent by the way)
App.ApplicationRoute = Ember.Route.extend({
    setupController: function(){
	    this.controllerFor('food').set('model',App.Food.find());
    }
});

#2

I typically use setupController to further refine model content for use in the view without adding any unnecessary clutter to the model.


#3

From the Ember.js Routing docs:

The default setupController hook sets the model property of the associated controller to the route handler’s model.

In your example it’s just setting the model for the Food controller even though you’re on the Application route.

As for the controllerFor, check this out (it’s from the same documentation page):

If you want to configure a controller other than the controller associated with the route handler, use the controllerFor method:

App.PostRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    this.controllerFor('topPost').set('content', model);
  }
});

#4

The model-hook is useful if you need to retrieve your model based on URL params.

model: function(params) {
   return App.Post.find(params.post_id);
}

#5

It’s worth noting that the model hook is actually the same thing as deserialize (only that deserialize is a private API).

For example if you transition with a context the model hook will not get called. It also gets called before setupController on all nested routes, which means you can’t use this.controllerFor inside the model hook.

here’s more detailed info


#6