Rollback has many relationship data?

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?

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);
  }

});

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();      
 
    }

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
    }
  }
});

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.

I tried this out but I can not make this work (addon). Can create an issue on github if you like.

There is also ember-data-change-tracker

1 Like

For future devs, this is an awesome addon that handles this and much more: GitHub - poteto/ember-changeset: Ember.js flavored changesets, inspired by Ecto