js-data icon indicating copy to clipboard operation
js-data copied to clipboard

Need to be able to cascade saves.

Open end-user opened this issue 9 years ago • 3 comments

When I load child objects (such as with parent.loadRelations(['child'])) and modify a property on the child object, I'd like to be able to persist the whole tree at once by calling parent.save(). Since I'm manually (lazy) loading with loadRelations(), perhaps I could also saveRelations()?

end-user avatar Aug 08 '16 17:08 end-user

Found a gem. Let's say parent hasMany child and child belongsTo parent. Let's also say that in memory, parent.children is a link and child.parent is a link. I'm thinking this should work:

child.save({ with: ['parent']).then(...)
// or
parent.save({ with: ['children'] }).then(...)

Can you try it?

jmdobry avatar Aug 08 '16 21:08 jmdobry

Yes, that does sort of work; it does push data to the server for the related objects. However, using with causes all the data to be sent to one url. This might work if you're coding your backend to split off those objects and store them separately. But, in the case of my mock server, json-server, that will actually store the children objects in two separate places and create data incongruities. I think the relationship would need to keep the resource's uri in order to act accordingly. (e.g. if it was originally retrieved from /parents/1/children, the collection should be put back there)

end-user avatar Aug 09 '16 13:08 end-user

So you want it to make multiple requests? Yeah, that would require more implementation in JSData.

jmdobry avatar Aug 09 '16 17:08 jmdobry