user1624400
user1624400

Reputation: 395

associating a model with a route in emberjs

As I understad, a template in emberjs gets it's data from controller. So, it's a controller's job to get hold of model data and present it to the template.

The docs here associate a model with a route like this:

App.FavoritesRoute = Ember.Route.extend({
  model: function() {
    // the model is an Array of all of the posts
    return App.Post.find();
  }
});

In this case and ArrayController is automatically generated.


However, there's also the setupController function. So, can we also do this :

App.FavoritesRoute = Ember.Route.extend({
  setupController: function(controller) {
    controller.set('model', App.Post.find());
  }
});

as the first example given here do?

Do the two ways do the same thing?

Upvotes: 0

Views: 50

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

Do the two ways do the same thing?

Almost. In both cases the controller's content property will be set to the result of App.Post.find(). And both will work.

That said, using the model hook is the preferred way to do this. If your model hook returns a promise, the router will wait for it to resolve before moving on. That is not the case with the setupController hook. generally you will want to avoid anything async from the setupController hook.

Upvotes: 1

Related Questions