DS.ManyArray Class addon/-private/system/many-array.js:12
Extends: Ember.Object
Uses: Ember.MutableArray
Uses: Ember.Evented
Defined in: addon/-private/system/many-array.js:12
Module: ember-data
A ManyArray
is a MutableArray
that represents the contents of a has-many
relationship.
The ManyArray
is instantiated lazily the first time the relationship is
requested.
Inverses
Often, the relationships in Ember Data applications will have an inverse. For example, imagine the following models are defined:
app/models/post.js | |
1 2 3 4 5 |
import DS from 'ember-data'; export default DS.Model.extend({ comments: DS.hasMany('comment') }); |
app/models/comment.js | |
1 2 3 4 5 |
import DS from 'ember-data'; export default DS.Model.extend({ post: DS.belongsTo('post') }); |
If you created a new instance of App.Post
and added
a App.Comment
record to its comments
has-many
relationship, you would expect the comment's post
property to be set to the post that contained
the has-many.
We call the record to which a relationship belongs the relationship's owner.
Methods
Properties
createRecord
(hash)
DS.Model
private
Create a child record within the owner
Parameters:
- hash Object
Returns:
- DS.Model
- record
loadedRecord
private
loadingRecordsCount
(count)
private
Parameters:
- count Number
reload
public
save
DS.PromiseArray
Saves all of the records in the ManyArray
.
Example
1 2 3 4 5 6 7 8 |
store.findRecord('inbox', 1).then(function(inbox) { inbox.get('messages').then(function(messages) { messages.forEach(function(message) { message.set('isRead', true); }); messages.save() }); }); |
Returns:
- DS.PromiseArray
- promise
isLoaded
Boolean
The loading state of this array
isPolymorphic
Boolean
private
true
if the relationship is polymorphic, false
otherwise.
meta
Object
public
Metadata associated with the request for async hasMany relationships.
Example
Given that the server returns the following JSON payload when fetching a hasMany relationship:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "comments": [{ "id": 1, "comment": "This is the first comment", }, { // ... }], "meta": { "page": 1, "total": 5 } } |
You can then access the metadata via the meta
property:
1 2 3 4 5 6 |
post.get('comments').then(function(comments) { var meta = comments.get('meta'); // meta.page => 1 // meta.total => 5 }); |
promise
Ember.RSVP.Promise
private
Used for async hasMany
arrays
to keep track of when they will resolve.
relationship
ManyRelationship
private
The relationship which manages this array.