10
votes

I'm using ember.js 1.2 and one of my routes has a very dynamic model. When I jump into the route for the first time or when I paste in the url the model hook of the route fires, then setup controller fires and everything works as expected.

The problem occurs when I come into the route later (but not from the url directly) -this only hits the setupController hook (and the model method never fires). But technically what's changed is the url (and the parent model). And with this model, it's primarily defined from the parent (using that parent model to fetch a new set of dynamic configuration at runtime).

So how can I force the setupController to re-execute the model hook each time this specific route is loaded? (as if the model method was firing each time).

Or ... Should I fetch this dynamic model in setupController and keep the model hook logic-less by having it return an empty object?

Update

App.ChildIndexRoute = Ember.Route.extend(App.ModelMixin, {
  setupController: function(controller, model) {
    this._super(controller, model);
    var parent = this.modelFor('parent');
    return this.getForParent(parent).then(function(things) {
      controller.set('model', things);
    });
  }
});
1

1 Answers

6
votes

You can use the setupController hook instead of the model hook, it's a perfectly acceptable way to handle it.

And technically the transition is what calls the model hook and supplies it to the setupController.

Where in the chain is it not firing the model hook? Here's a simple app with a few nested resources.

http://emberjs.jsbin.com/AtebAsOS/6/edit

The key bit of code in this example is in the DogsRoute:

App.DogsRoute = Em.Route.extend({
  setupController: function(controller, model){
    model = Em.get(this.modelFor('cow'), 'dogs');
     this._super(controller, model); 
  }
});

From the docs:

[The setupController] method is called with the controller for the current route and the model supplied by the model hook.

So when you override the model with the fetched dogs model and pass it to _super, the controller will use the freshly fetched model.