Last active
September 15, 2015 12:27
-
-
Save olivierchatry/3bd056fae536e6830378 to your computer and use it in GitHub Desktop.
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
| destroyWithRelation(model, relations) { | |
| let previousPromise = null; | |
| let values = [] | |
| relations.forEach( (relation) => { | |
| if (previousPromise) { | |
| previousPromise = previousPromise.then( | |
| () => { return model.get(relation) } | |
| ) | |
| } else { | |
| previousPromise = model.get(relation) | |
| } | |
| previousPromise.then( | |
| (v) => values.push(v) | |
| ) | |
| } | |
| ) | |
| previousPromise.then( | |
| () => { return model.destroyRecord() } | |
| ) | |
| for (let i = 0; i < relations.length; ++i) { | |
| previousPromise = previousPromise.then( | |
| () => { | |
| let value = values[i] | |
| if (Ember.isArray(value)) { | |
| return new Promise( (resolve, reject) => { | |
| let count = value.length | |
| if (count > 0) { | |
| value.forEach( | |
| (v) => { | |
| v.save().then( | |
| () => { | |
| if (--count === 0) resolve() | |
| } | |
| ) | |
| } | |
| ) | |
| } else { | |
| resolve() | |
| } | |
| } | |
| ) | |
| } else { | |
| if (!Ember.isNone(value)) { | |
| return value.save() | |
| } | |
| } | |
| } | |
| ) | |
| } | |
| return previousPromise | |
| }, | |
| destroyTheModel() { | |
| return this.destroyWithRelation(model, ['1relaton', '2relation', '3relation']) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment