Revert a Model to its Previous Values

Is there any easy way to revert a model to its previously values saved in the database?

Fortunately sugar has made most of our job easier…:)

To revert back a model to its previous values, you can just add the below line to your js code.

model.revertAttributes();
view raw revetmodel.js hosted with ❤ by GitHub

If you want to check whether any of the attributes has changed or not. You can do it by adding the below line.

 

var changedAttributes = model.changedAttributes(model.getSyncedAttributes());
if (!_.isEmpty(changedAttributes)) {
//some attributes's value has changed
}

Note: model represents the backbone model which corresponds to your sugar record.

If you are trying this in record.js controller, you can replace model with this.model.

Follow my blog to get daily tips……..:)


Leave a comment