I have the following in my coffeescript/backbone app. The problem is it’s running console.log BEFORE the “each” function is complete (in other words, before it’s done doing create() for each collection). What I would like to know is if there is some type of success callback I can use that I’m not aware of in order to run console.log once the creates are complete. Thanks in advance!
wishlist.each (model) ->
id = model.get('id')
lineitems.create({product_id: id})
console.log "do this when finished!"
I think you are confused. The
eachfunction onBackbone.Collections is not asynchronous and it doesn’t trigger any AJAX calls. Theeachfunction actually comes from Underscore which isn’t concerned with async functionality.The asynchronous AJAX call that fetches the data is called
fetchand it does have a success/error callback:EDIT
OK, so you want
console.logto execute after ALL creations have completed. First of all, what I said aboutfetchis true of create. You can addsuccessanderrorcallbacks.I would consider incrementing a count for every
createand decrementing the count on everysuccessanderrorcallbacks. When you have finished creating AND your count reaches 0, you have processed all the creations and you can call it back.