I\'m having difficult saving a one-to-many relationship in ember data. I have a relationship like this:
App.ParameterSet = DS.Model
name: DS.attr(\"strin
I'm not able to add comments to ahmacleod's answer, but it is spot on except that I noticed that the parent record is not being marked as dirty when the child record is modified. In the example in the question, the issue doesn't arise because the name is also modified on the parent record.
In general, though, if you're going to follow ahmacleod's second answer, you need to override the method dirtyRecordsForHasManyChange on the RESTAdapter. Otherwise, addHasMany in the serializer is never called, since the record is not even marked as dirty.
the existing method looks like:
dirtyRecordsForHasManyChange: function(dirtySet, record, relationship) {
var embeddedType = get(this, 'serializer').embeddedType(record.constructor, relationship.secondRecordName);
if (embeddedType === 'always') {
relationship.childReference.parent = relationship.parentReference;
this._dirtyTree(dirtySet, record);
}
},
So I guess you'll want something like:
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.extend({
dirtyRecordsForHasManyChange: function(dirtySet, record, relationship) {
relationship.childReference.parent = relationship.parentReference;
this._dirtyTree(dirtySet, record);
},
serializer: DS.RESTSerializer.extend({
addHasMany: function(hash, record, key, relationship) {
// custom ...
}
})
})
});
The serialization of hasMany
relationships is handled by the addHasMany()
method of json_serializer.js
.
The following note is included in the source code:
The default REST semantics are to only add a has-many relationship if it is embedded. If the relationship was initially loaded by ID, we assume that that was done as a performance optimization, and that changes to the has-many should be saved as foreign key changes on the child's belongs-to relationship.
To achieve what you want, one option is to indicate that the relationship should be embedded in your adapter.
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.extend({
serializer: DS.RESTSerializer.extend({
init: function() {
this._super();
this.map('App.ParameterSet', {
regions: { embedded: 'always' }
});
}
})
})
});
Of course now your back-end will need to actually embed the associated regions' JSON within the parameter set's JSON. If you want to keep things as they are, you can simply override addHasMany()
with custom serialization.
App.Store = DS.Store.extend({
adapter: DS.RESTAdapter.extend({
serializer: DS.RESTSerializer.extend({
addHasMany: function(hash, record, key, relationship) {
// custom ...
}
})
})
});