How to rollback relationship changes in EmberData

后端 未结 4 1136
春和景丽
春和景丽 2020-12-31 19:08

I have two models with parent-child relationship: training and exercise:

App.Training = DS.Model.extend({
  exercises:         


        
4条回答
  •  鱼传尺愫
    2020-12-31 19:29

    @tothda and other readers to follow. As of Ember Data : 1.0.0-beta.10+canary.7db210f29a the parent is still not designed to make parentTraining.isDirty() a value of true when a child is rolled back. Ember Data does consider a parent record to be dirty when an attribute is changed, but not when a DS.hasMany array has changes (this allows save() to work, so you can updated any changes to the parent's attributes on the server).

    The way around this for the case mentioned, where you want to do a rollback() on a newly created child, is to replace the .rollback() with a .deleteRecord() on the child record you want to discard. Ember Data then automatically knows to remove it from the DS.hasMany array then, and you can pat yourself on the back for a rollback well done!

提交回复
热议问题