Revert model changes without Ember-Data Revert model changes without Ember-Data json json

Revert model changes without Ember-Data


It really depends on how your models are structured. I wrote my own Ember persistence library and I made every property a computed property (like Ember-Data). From there, I had two hashes in each model, oldProperties and newProperties. I use the two in tandem to read, and only write to newProperties. That way I can always restore to oldProperties if necessary.

The bottom line is that Ember itself doesn't store old property values for you. You'll have write your own custom logic to store the old values and restore them when you need.


Something that worked for me is to clone the model and then revert back to the cloned version if the user cancels the edit.

In the route

listEditController.set('clonedModelContent', Ember.copy(modelContent));

Then in the controller's cancel action:

close: function () {  Ember.setProperties(@get('model'), @get('clonedModelContent'));  // navigate somewhere else}

The pattern could be reverted in case you don't want to modify the model until the changes are 'accepted'. In that case, you set the clone to the controller's property, and when you save then copy the properties and cancel would simply navigate away, essentially discarding the changes in the cloned object.

Hope this helps.


I like to use the rollbackAttributes() method on the model.

import Ember from 'ember';export default Ember.Component.extend({actions: {            cancelEdit: function() {        var model = this.get('objectBeingEdited');        model.rollbackAttributes();        this.toggleProperty('isEditing');        }    }});