Skip to content

Instantly share code, notes, and snippets.

@olivierchatry
Last active September 15, 2015 12:27
Show Gist options
  • Select an option

  • Save olivierchatry/3bd056fae536e6830378 to your computer and use it in GitHub Desktop.

Select an option

Save olivierchatry/3bd056fae536e6830378 to your computer and use it in GitHub Desktop.
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