DS#loadRelations

DS#loadRelations(resourceName, instance|id, relations[, options])

Asynchronously load the indicated relations for the given item.

Returns a promise.

Arguments
nametypedescription
resourceNamestringThe name of the resource to use. Unnecessary if using the resource directly.
idstring or numberThe id of the instance for which to load relations.
relationsstring or arrayThe resource names or field names of relations to load. If not provided then load all relations.
optionsobjectConfiguration options. Also passed through to the adapter and (conditionally) to DS.inject.
options.adapterstringOverride the default adapter.
options.cacheResponsebooleanInject 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.