Eagerly load deeply nested ember-data relationships
MIT License
Preloading deeply nested relationships is possible w/ ember-data, but it can be a little messy
For example, if we have a List
model, each record of which has many ListItem
s, each of which has an Assignee
, and we wanted to load all of the relevant data by the time the route was done resolving, we'd have to do something like this:
Ember.Route.extend({
model() {
// Load lists
return this.store.findAll('lists').then((lists) => {
// Get promises for loading all list-items owned by the lists,
// and wait for them all to resolve
return Ember.RSVP.all(
lists.map((l) => l.get('list-item'))
.reduce((prev, cur) => prev.concat(cur))
).then((listItems) => {
// Get promises for loading all assignees of the list-items,
return Ember.RSVP.all(
listItems.map((listItem) => listItem.get('assignee'))
.reduce((prev, cur) => prev.concat(cur))
);
}).then(() => lists);
});
}
});
First, install this addon in your ember-cli app
ember install ember-data-preload
And then, you may preload relationships (i.e., in a route)
import preload from 'ember-data-preload';
export default Ember.Route.extend({
model() {
return preload(this.store.findAll('lists'), {'list-items': 'assignees'});
}
});
Particularly when relationship hierarchies are deep, this can be particularly time-saving
import preload from 'ember-data-preload';
export default Ember.Route.extend({
model() {
return preload(this.store.findAll('country'), {
city: {
neighborhood: {
street: 'house'
}
}
});
}
});
This README outlines the details of collaborating on this Ember addon.
git clone
this repositorynpm install
bower install
ember server
npm test
(Runs ember try:testall
to test your addon against multiple Ember versions)ember test
ember test --server
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.