Skip to content

Instantly share code, notes, and snippets.

@nickiaconis
Created October 14, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickiaconis/7b045c65942dbb118097 to your computer and use it in GitHub Desktop.
Save nickiaconis/7b045c65942dbb118097 to your computer and use it in GitHub Desktop.
Route#prefetch & Route#prefetched
App.PostRoute = Ember.Route.extend({
prefetch(params) {
return this.store.findRecord('post', params.id);
}
});
App.PostCommentsRoute = Ember.Route.extend({
prefetch(params, transition) {
return this.store.findAll('comments', transition.params.post.id);
},
async model() {
return {
OP: (await this.prefetched('post')).author,
comments: await this.prefetched(this.routeName)
};
}
]);
@nickiaconis
Copy link
Author

Route#prefetch hook loads data in parallel.

Route#prefetched method gets the promise returned from Route#prefetch for a given route name.

Default Route#model hook returns the promise returned by Route#prefetch if it exists, otherwise performs the existing default.

Can almost be accomplished with Router#willTransition public API, a reserved property on Route (I call it asyncData, but that could be changed), and one reopen:

  1. Add a willTransition subscriber to router:main, which synchronously calls all Route#prefetch hooks for the transition, storing the result at Route#asyncData. (Currently, broken. Need a willTransition like thing that also fires on transition redirects.)
  2. Reopen Route to add Route#prefetched method and extend default Route#model hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment