I’m using Ember Data to define two models and an association between the two, and then inserting some data using the createRecord method. The attributes that don’t involve the association are present in the model and gettable (they return the proper values), but a call to the association’s content returns an empty array. What am I doing wrong?
Edit: So using load instead of createRecord now gives an array of [3,3], and I still cannot access the attributes. The closest I can get is: b.get("messages").get("firstObject").id, which returns the string “[object Object]”
// Store
App.store = DS.Store.create({
revision: 6,
adapter: App.adapter
});
// Models
App.Message = DS.Model.extend({
msg: DS.attr('string'),
time: DS.attr('string'),
chat: DS.belongsTo('App.Chat')
});
App.Chat = DS.Model.extend({
name: DS.attr('string'),
avatar: DS.attr('string'),
messages: DS.hasMany('App.Message', { embedded: true }),
preview: function() {
this.get('messages').firstObject;
}.property('messages')
});
var a = App.store.createRecord(App.Chat, { "id": 1,
"name": 'Foo Bar',
"avatar": 'test',
"messages": [{
"id": 1,
"msg": 'This is a test post one two three',
"time": '1:10pm'
},{
"id": 2,
"msg": ' This is another test post three two one',
"time": '1:15pm'
}]
});
var b = App.store.find(App.Chat, 1);
console.log(b.get("name")) // Foo Bar
console.log(b.get("messages").get("content")) // [] (an empty array)
Looks like support for
{embedded: true}was removed not too long ago, and that is the reason why this isn’t working. There’s a pull request (that works) that’s currently pending that brings this functionality back.https://github.com/emberjs/data/pull/428