DS#loadRelations
DS#loadRelations(resourceName, instance|id, relations[, options])
Asynchronously load the indicated relations for the given item.
Returns a promise.
Arguments
name | type | description |
---|---|---|
resourceName | string | The name of the resource to use. Unnecessary if using the resource directly. |
id | string or number | The id of the instance for which to load relations. |
relations | string or array | The resource names or field names of relations to load. If not provided then load all relations. |
options | object | Configuration options. Also passed through to the adapter and (conditionally) to DS.inject . |
options.adapter | string | Override the default adapter. |
options.cacheResponse | boolean | Inject the result into the store. Default: true . |
Examples
var store = new JSData.DS();
var User = store.defineResource({
name: 'user',
relations: {
hasOne: {
profile: {
localField: 'profile',
foreignKey: 'userId'
}
}
}
});
var Profile = store.defineResource({
name: 'profile',
relations: {
belongsTo: {
user: {
localField: 'user',
localKey: 'userId'
}
}
}
});
User.loadRelations(10, ['profile']).then(function (user) {
user.profile; // object
assert.deepEqual(user.profile, Profile.filter({ userId: 10 })[0]);
});
var user = store.get('user', 10);
User.loadRelations(user, ['profile']).then(function (user) {
user.profile; // object
assert.deepEqual(user.profile, Profile.filter({ userId: 10 })[0]);
});
User.loadRelations(10, ['profile'], { cacheResponse: false }).then(function (user) {
user.profile; // object
assert.equal(Profile.filter({ userId: 10 }).length, 0);
});
Additional reading:
Need help?
Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.
Updated less than a minute ago