Created
October 14, 2015 21:39
-
-
Save nickiaconis/7b045c65942dbb118097 to your computer and use it in GitHub Desktop.
Route#prefetch & Route#prefetched
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| }; | |
| } | |
| ]); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Route#prefetchhook loads data in parallel.Route#prefetchedmethod gets the promise returned fromRoute#prefetchfor a given route name.Default
Route#modelhook returns the promise returned byRoute#prefetchif it exists, otherwise performs the existing default.Can almost be accomplished with
Router#willTransitionpublic API, a reserved property on Route (I call it asyncData, but that could be changed), and one reopen:router:main, which synchronously calls allRoute#prefetchhooks for the transition, storing the result atRoute#asyncData. (Currently, broken. Need a willTransition like thing that also fires on transition redirects.)Routeto addRoute#prefetchedmethod and extend defaultRoute#modelhook.