4

I have a route that requires extra reference data as well as the regular model for drop downs and alike. Where is the best place to put this? I was thinking that maybe the afterModel hook but I am not sure.

afterModel : function(site, transition) {
    this.store.find('stuff', {site : site.get('id')});
    this.store.find('moreStuff');
}
jax
  • 37,735
  • 57
  • 182
  • 278

2 Answers2

3

Generally the setupController hook is the appropriate place to do this, since you'll generally be attaching this reference data to the controller for use there. Although, you can block the transition by returning a promise to the afterModel hook, if you need this data to be available before the controller is set up, though you'll need to keep track of the values in the route, then manually assign them in setupController.

setupController: function(controller, model){
  this._super(controller, model);
  controller.set('stuff', this.store.find('stuff', {site : site.get('id')}));
}

or if you use afterModel

afterModel : function(site, transition) {
   var self = this,
       store = this.store;
   return Em.RSVP.hash({
     stuff: store.find('stuff', {site : site.get('id')}),
     more: store.find('moreStuff')
   }).then(function(hash){
     self.set('extraCrap', hash);
   });
},

setupController: function(controller, model){
  this._super(controller, model);
  controller.set('stuff', this.get('extraCrap.stuff'));
}

You can also look at this, but be sure to read the second answer: EmberJS: How to load multiple models on the same route?

Community
  • 1
  • 1
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
0

What I find the best approach and i'm also using for these exact cases is the hash function of Ember.RSVP within the route's model function.

http://emberjs.com/api/classes/Ember.RSVP.html#method_hash

This way the route is only loaded when all required resources are available. It is also handy to take advantage of the loading substate (http://emberjs.com/guides/routing/loading-and-error-substates/).

Usually I also proxy the calls from a specific controller's functions that handles caching, as lookup values hardly change within a session. The functions return the promise directly if the requested lookup values are not found in cache.

The other option, if needed to avoid Ember.RSVP would probably be in setupController or afterModel functions but would have to take care of displaying a modal loading spinner until loading had been completed.

melc
  • 11,523
  • 3
  • 36
  • 41