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("string")
regions: DS.hasMany("App.Region")
App.Region = DS.Model
name: DS.attr("string")
If I were to do something like this:
parameterSet = App.ParameterSet.find(5)
@transaction = @get("store").transaction()
@transaction.add(parameterSet)
region1 = App.Region.find(10)
region2 = App.Region.find(11)
parameterSet.set("name", "foo")
parameterSet.get("regions").pushObject(region)
@transaction.commit()
Then I would like to see a PUT request with payload like this:
api/ParameterSets/5
{parameterSet: {name: "foo", regionIds:[10, 11]}}
but instead I get this:
{parameterSet: {name: "foo"}}
I don't care about the relationship back from child to parent but if I add parameterSet: DS.belongsTo("App.ParameterSet")
to the App.Region model then I get 2 PUT requests to the regions url for the two new relationships which is not really what I want.
I guess this is a many-to-many relationship really which I'm not sure is yet supported but any ideas on how to achieve what I've described? Thanks
The serialization of
hasMany
relationships is handled by theaddHasMany()
method ofjson_serializer.js
.The following note is included in the source code:
To achieve what you want, one option is to indicate that the relationship should be embedded in your adapter.
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.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:
So I guess you'll want something like: