I started looking for a built-in recording mixin to serialize / deserialize records with hasMany / belongs to relationships.
Model:
App.Post= DS.Model.extend({
comment: DS.belongsTo('comment'),
name: DS.attr('string')
});
App.Comment =DS.Model.extend({
post: DS.belongsTo('Post'),
value: DS.attr('string')
});
Serializer:
App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
comments: {embedded: 'always'}
}
});
When I try to save the model, I got the following error.
var model = this.store.createRecord('post');
model.get('comments').pushObject(this.store.createRecord('comment'));
model.setProperties({
name: 'test'
});
model.save();
Error: approval failed: you must specify the identifier for App.Comment in the object passed for push. Only an error in the browser console, but on the backend, the data goes as expected.
So, I started looking for a workaround and found something interesting in https://gist.github.com/vampolo/8f8237cce2b8a52ab9d4 .
source: http://vincenzo-ampolo.net/2014/09/21/ember-data-embedded-records-without-ids-ready-to-go/
Following this link, I changed my serializer as follows.
Serializer (changed):
App.PostSerializer=DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,DS.NoKeyMixin, {
attrs: {
comments: {embedded: 'always',noKey: true}
}
});
, .
Uncaught TypeError: embeddedRecord.serialize .
. .