Last active
April 28, 2016 19:56
-
-
Save offirgolan/117f61081662a522276a760cf473955e to your computer and use it in GitHub Desktop.
Rollback model attributes as well as its relationships
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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