Let’s say you have
Batman.AssociationSet, or even a plain JS array) whose members are
Batman.Model instances. You have a
saveAll function that saves all the members:
class ThingsController extends Batman.Controller saveAll: (setOfRecords) -> setOfRecords.forEach (record) -> record.save()
But what if you wanted to call a
callback when the whole operation was finished?
- Set up a listener on the
onceto avoid observer bloat.
- Before each save operation,
- When each one finishes,
That way, it will get prevented n times – once for each item in the set – and it will finally be fired when the last operation is finished.
Here it is, all together:
class ThingsController extends Batman.Controller saveAll: (setOfRecords, callback) -> # observer: @once 'finished', -> callback() setOfRecords.forEach (record) => # mind the fat arrows! @prevent('finished') record.save (err, record) => @allowAndFire('finished')
@allowAndFire are provided by
Batman.EventEmitter, so it will only work with objects which have that mixin. Don’t worry – every object in batman.js is an event emitter! But plain JS objects are not.