In several routes in throughout our application, we rely on model
hooks to fetch some additional, related data, usually via an Ember.RSVP.hash
:
model: function() {
return Ember.RSVP.hash({
model: this.store.find('post', params.post_id),
trendingPosts: this.store.find('post', { trending: true })
});
},
setupController: function(controller, hash) {
controller.set('model', hash.model)
controller.set('trendingPosts', hash.trendingPosts);
}
However, this get’s problematic when we try to use a {{#link-to 'posts.index' postRecord}}
, since the model
hook never fires, so the additional data is never fetched.
We tried limiting the model
hook to only fetch the resource for that route (i.e. fetch the model you’d pass in to link-to
) and instead use afterModel
to fetch additional data:
model: function() {
return this.store.find('post', params.post_id);
},
afterModel: function() {
return this.store.find('post', { trending: true })
}
This feels much cleaner, but now there’s no way to get that additional data (trendingPosts
) attached to the controller. setupController
gets the results of the model
hook, not the afterModel
hook.
The only way around this I can see would be to set a property on the route as a temporary storage until setupController
is called, but that’s a pretty ugly hack:
model: function() {
return this.store.find('post', params.post_id);
},
afterModel: function() {
var _this = this;
return this.store.find('post', { trending: true }).then(function (trendingPosts) {
_this.set('trendingPosts', trendingPosts);
});
},
setupController: function(controller, model) {
controller.set('model', model)
controller.set('trendingPosts', this.get('trendingPosts'))
}
Any suggestions on the best way to fetch additional data?