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 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) | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Route#prefetch
hook loads data in parallel.Route#prefetched
method gets the promise returned fromRoute#prefetch
for a given route name.Default
Route#model
hook returns the promise returned byRoute#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:router:main
, which synchronously calls allRoute#prefetch
hooks for the transition, storing the result atRoute#asyncData
. (Currently, broken. Need a willTransition like thing that also fires on transition redirects.)Route
to addRoute#prefetched
method and extend defaultRoute#model
hook.