Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
use promises to batch delete things. Resolve when all complete
save: ->
# avoid making a request before one comes back
return if @prop('busy')
@prop('busy', true)
# we want to delete first so that when we call save we get updated response without the deleted models
@delete().then =>
.then (models) =>
# # add the response of models to the collection
@reset(models) if models.length
.catch (xhr) =>
for error in XhrErrors.parseTransportErrors(xhr)
if error.field
error = error.message
resource : @parentModel.resource
content : error
severity : 'error'
finishSave: ->
@prop('busy', false)
@prop('inEditMode', false)
delete: ->
return Promise.resolve() unless @prop('modelsToDelete').length
@gatherPromises().then (promiseBatches) ->
# flattens aray of promise arrays
promises = [].concat.apply([], promiseBatches)
Promise.all promises
delayedBatchDelete: (resolvers) ->
setTimeout =>
if @prop('modelsToDelete').length
deleteBatch: (resolvers) ->
deleteBatch = @prop('modelsToDelete').splice(0, @TAG_DELETE_RATE_LIMIT)
resolve = resolvers[0]
resolve (model.destroy(silent:true) for model in deleteBatch)
gatherPromises: ->
resolvers = []
promises = []
batchCount = Math.ceil(@prop('modelsToDelete').length/@TAG_DELETE_RATE_LIMIT)
for i in [1..batchCount]
promise = new Promise (resolve) ->
resolvers.push resolve
promises.push promise
# delete first 10 and delay the remainin batches
@delayedBatchDelete(resolvers) if batchCount > 1
Promise.all promises
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.