Rollback has many relationship data?


#1

i have to rollback data which has has many relationship . rollback does not support for relationship. how to deal with this as i have to rollback all the removed values when not saved?


#2

You will have to handle this manually.

For example, you could add two methods to your model:

export default DS.Model.extend({

  items: DS.hasMany('item'),

  pinRelationships() {
    let pinnedRelationships = {
      items: this.get('items').toArray()
    };

    this.set('_pinnedRelationships', pinnedRelationships);
  },

  rollbackRelationships() {
    let items = this.get('_pinnedRelationships.items');
    this.set('items', items);
  }

});

#3

how to call this into controller??

in model i have created this method but i have to call in the remove action in the controller

remove: function() {  
 
        this.get('model').rollbackAttributes();      
 
    }

#4

Well, you would use my above code snippet something like this (in the route):

export default Ember.Route.extend({
  model() {
     return this.get('store').findRecord('my-model', 1);
  },

  afterModel(model) {
    model.pinRelationships(); // Save the state that you want to be able to go back to
  },

  actions: {
    cancel() {
      let model = this.currentModel;
      model.rollbackAttributes(); // Rollback the attributes
      model.rollbackRelationships(); // Actually go back to the previously saved state of relationships
    }
  }
});

#5

Late to the party, but here we go:

I created an addon that resolves this issue. Just call rollbackRelationships() and it will rollback all your relationships. Look at the README for more options.