Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best approach to "force" the model hook to fire?

Tags:

ember.js

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);
    });
  }
});
like image 841
Toran Billups Avatar asked Dec 01 '13 20:12

Toran Billups


1 Answers

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.

like image 97
Kingpin2k Avatar answered Nov 21 '22 04:11

Kingpin2k