Defined in: addon/-private/system/references/has-many.js:16
Module: ember-data
A HasManyReference is a low level API that allows users and addon author to perform meta-operations on a has-many relationship.
String
The link Ember Data will use to fetch or reload this has-many relationship.
Example
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
links: {
related: '/posts/1/comments'
}
}
}
}
});
var commentsRef = post.hasMany('comments');
commentsRef.link(); // '/posts/1/comments'
String Promise
Loads the relationship if it is not already loaded. If the relationship is already loaded this method does not trigger a new load.
Example
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
var commentsRef = post.hasMany('comments');
commentsRef.load().then(function(comments) {
//...
});
Promise Object
The link Ember Data will use to fetch or reload this has-many relationship.
Example
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
links: {
related: {
href: '/posts/1/comments',
meta: {
count: 10
}
}
}
}
}
}
});
var commentsRef = post.hasMany('comments');
commentsRef.meta(); // { count: 10 }
Object DS.ManyArray
push can be used to update the data in the relationship and Ember Data will treat the new data as the canonical value of this relationship on the backend.
Example
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
var commentsRef = post.hasMany('comments');
commentsRef.ids(); // ['1']
commentsRef.push([
[{ type: 'comment', id: 2 }],
[{ type: 'comment', id: 3 }],
])
commentsRef.ids(); // ['2', '3']
Array|Promise
Promise
Reloads this has-many relationship.
Example
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
var commentsRef = post.hasMany('comments');
commentsRef.reload().then(function(comments) {
//...
});
Promise Array
ids() returns an array of the record ids in this relationship.
Example
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
var commentsRef = post.hasMany('comments');
commentsRef.ids(); // ['1']
Array DS.ManyArray
value() sycronously returns the current value of the has-many 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
app/models/post.jsexport default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var post = store.push({
data: {
type: 'post',
id: 1,
relationships: {
comments: {
data: [{ type: 'comment', id: 1 }]
}
}
}
});
var commentsRef = post.hasMany('comments');
post.get('comments').then(function(comments) {
commentsRef.value() === comments
})
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
http://emberjs.com/api/data/classes/DS.HasManyReference.html