Correct way to assign models to an ArrayController


#1

I found two different ways to assign an array of models to an ArrayController. First by setting the controller content in the route:

App.PostsRoute = Ember.Route.extend({
  setupController: function(controller) {
    var days =  [this.store.createRecord('comment'), ...];
    controller.set('content', days);
  }
});

And secondly by creating a calculated property in the controller:

App.PostsController = Ember.ArrayController.extend({
  comments: function() {
    return [this.store.createRecord('comment'), ...];
  }.property()
});

Is there a right and a wrong way? Is one more idiomatic as the other?


#2

you probably want to set the model property on the route instead of have it in the setup:

App.PostsRoute = Ember.Route.extend({
  model: function(params) {
    return  [];
  }
});

#3

I saw a great presentation the other day where it was said that if you’re dealing with persistent data, you should handle in the route. If you’re dealing with temporary data, you should handle it in the controller. The rationale is that you have a lot more visibility into other controllers/routes at the route level.

For example, you can do something like…

model: function(){
   return this.modelFor("SomeOtherRoute");
}

Which keeps your code a little more DRY.

The biggest benefit (that I see) to using setupController is that it fires every time you access the route. The state of stuff declared directly in the controller persists between requests.

I’m still pretty new at this so I’m open to corrections, but that’s how I’ve been treating the route/controller relationship.