DS.Model#belongsTo()

belongsTo (name) BelongsToReference

Defined in addon/-private/system/model/model.js:823
Available since 2.5.0

Get the reference for the specified belongsTo relationship.

Example

// models/blog.js
export default DS.Model.extend({
  user: DS.belongsTo({ async: true })
});

var blog = store.push({
  type: 'blog',
  id: 1,
  relationships: {
    user: { type: 'user', id: 1 }
  }
});
var userRef = blog.belongsTo('user');

// check if the user relationship is loaded
var isLoaded = userRef.value() !== null;

// get the record of the reference (null if not yet available)
var user = userRef.value();

// get the identifier of the reference
if (userRef.remoteType() === "id") {
  var id = userRef.id();
} else if (userRef.remoteType() === "link") {
  var link = userRef.link();
}

// load user (via store.findRecord or store.findBelongsTo)
userRef.load().then(...)

// or trigger a reload
userRef.reload().then(...)

// provide data for reference
userRef.push({
  type: 'user',
  id: 1,
  attributes: {
    username: "@user"
  }
}).then(function(user) {
  userRef.value() === user;
});

Parameters:

name String
of the relationship

Returns:

BelongsToReference
reference for this relationship
doc_EmberJs
2016-11-30 16:50:05
Comments
Leave a Comment

Please login to continue.