DS.BelongsToReference Class
A BelongsToReference is a low-level API that allows users and addon author to perform meta-operations on a belongs-to relationship.
Methods
id
()
String
The id
of the record that this reference refers to. Together, the
type()
and id()
methods form a composite key for the identity
map. This can be used to access the id of an async relationship
without triggering a fetch that would normally happen if you
attempted to use record.get('relationship.id')
.
Example
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
data: { type: 'user', id: 1 }
}
}
}
});
let userRef = blog.belongsTo('user');
// get the identifier of the reference
if (userRef.remoteType() === "id") {
let id = userRef.id();
}
Returns:
The id of the record in this belongsTo relationship.
link
()
String
`
javascript
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
links: {
related: '/articles/1/author'
}
}
}
}
});
let userRef = blog.belongsTo('user');
// get the identifier of the reference
if (userRef.remoteType() === "link") {
let link = userRef.link();
}
`
Returns:
load
-
options
Loads a record in a belongs to relationship if it is not already loaded. If the relationship is already loaded this method does not trigger a new load.
Example
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
data: { type: 'user', id: 1 }
}
}
}
});
let userRef = blog.belongsTo('user');
userRef.value(); // null
userRef.load().then(function(user) {
userRef.value() === user
});
You may also pass in an options object whose properties will be
fed forward. This enables you to pass adapterOptions
into the
request given to the adapter via the reference.
Example
userRef.load({ adapterOptions: { isPrivate: true } }).then(function(user) {
userRef.value() === user;
});
export default ApplicationAdapter.extend({
findRecord(store, type, id, snapshot) {
// In the adapter you will have access to adapterOptions.
let adapterOptions = snapshot.adapterOptions;
}
});
Parameters:
-
options
Objectthe options to pass in.
Returns:
a promise that resolves with the record in this belongs-to relationship.
meta
()
Object
`
javascript
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
links: {
related: {
href: '/articles/1/author',
meta: {
lastUpdated: 1458014400000
}
}
}
}
}
}
});
let userRef = blog.belongsTo('user');
userRef.meta() // { lastUpdated: 1458014400000 }
`
Returns:
push
-
objectOrPromise
push
can be used to update the data in the relationship and Ember
Data will treat the new data as the conanical value of this
relationship on the backend.
Example
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
data: { type: 'user', id: 1 }
}
}
}
});
let userRef = blog.belongsTo('user');
// provide data for reference
userRef.push({
data: {
type: 'user',
id: 1,
attributes: {
username: "@user"
}
}
}).then(function(user) {
userRef.value() === user;
});
Parameters:
-
objectOrPromise
Object | Promisea promise that resolves to a JSONAPI document object describing the new value of this relationship.
Returns:
A promise that resolves with the new value in this belongs-to relationship.
reload
-
options
Triggers a reload of the value in this relationship. If the
remoteType is "link"
Ember Data will use the relationship link to
reload the relationship. Otherwise it will reload the record by its
id.
Example
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
data: { type: 'user', id: 1 }
}
}
}
});
let userRef = blog.belongsTo('user');
userRef.reload().then(function(user) {
userRef.value() === user
});
You may also pass in an options object whose properties will be
fed forward. This enables you to pass adapterOptions
into the
request given to the adapter via the reference. A full example
can be found in the load
method.
Example
userRef.reload({ adapterOptions: { isPrivate: true } })
Parameters:
-
options
Objectthe options to pass in.
Returns:
a promise that resolves with the record in this belongs-to relationship after the reload has completed.
remoteType
()
String
`
app/models/post.js
export default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
`
`
javascript
let post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
let commentsRef = post.hasMany('comments');
// get the identifier of the reference
if (commentsRef.remoteType() === "ids") {
let ids = commentsRef.ids();
} else if (commentsRef.remoteType() === "link") {
let link = commentsRef.link();
}
`
Returns:
value
()
DS.Model
value()
synchronously returns the current value of the belongs-to
relationship. Unlike record.get('relationshipName')
, calling
value()
on a reference does not trigger a fetch if the async
relationship is not yet loaded. If the relationship is not loaded
it will always return null
.
Example
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
let blog = store.push({
data: {
type: 'blog',
id: 1,
relationships: {
user: {
data: { type: 'user', id: 1 }
}
}
}
});
let userRef = blog.belongsTo('user');
userRef.value(); // null
// provide data for reference
userRef.push({
data: {
type: 'user',
id: 1,
attributes: {
username: "@user"
}
}
}).then(function(user) {
userRef.value(); // user
});
Returns:
the record in this relationship