Loading Partial Models with Emberjs
Problem: How can I fetch an index of shallow models and flesh them out when needed?
Example: you have an index of stories to display but you don’t want to fetch all the stories’ content on the index route.
If your backend api is following the json:api spec. You can use Sparse Fieldsets.
Given the following routes:
This should only fetch the attributes you need to display your index of stories.
Then on the individual story route (e.g /stories/1
), you can use setupController to flesh out your model with extra data.
The model hooks blocks the UI, however the subsequent fetch in setupController will not. This means that if you launch the app at /stories and that navigate to /stories/1 you might see that the stories’ content is missing for a few seconds.
This is actually wanted. We render a part of the UI and then wait for the rest of the UI to load. This is a good place to put a content placeholder while the stories content is being fetched. This is would be a good use case for ember-concurrency.