Skip to content

Instantly share code, notes, and snippets.

@offirgolan
Last active April 28, 2016 19:56
Show Gist options
  • Save offirgolan/117f61081662a522276a760cf473955e to your computer and use it in GitHub Desktop.
Save offirgolan/117f61081662a522276a760cf473955e to your computer and use it in GitHub Desktop.
Rollback model attributes as well as its relationships
function rollbackModel() {
const model = this.get('model');
if (isArray(model)) {
model.forEach(m => this._deepRollback(m));
} else {
this._deepRollback(model);
}
}
function _deepRollback(model) {
model.reload().then(m => {
m.eachRelationship((key, relationship) => {
if (relationship.kind === 'belongsTo') {
this._rollBack(get(m, key));
} else if (relationship.kind === 'hasMany') {
get(m, key).forEach(m => this._rollBack(m));
}
});
});
}
function _rollBack(model) {
if(!isNone(model)) {
model = get(model, 'content') || model;
if(model.rollbackAttributes && model.get('hasDirtyAttributes')) {
model.rollbackAttributes();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment