Reputation: 677
I'm trying to perform some action, when all event handlers for some event are done.
My idea was to pass an object in event:
self.trigger("my_event", {handlers: x, callback: function});
And in the handlers I'd write something like:
handler = function(lock){
lock.handlers --;
if (lock.handlers == 0){
lock.callback();
}
}
(obviously that'd be the lock object's responsibility, but you can see the idea)
But I don't know how to get information about number of handlers (x
in my example) registered for the event. Is this possible? Is there some other way to do this?
Upvotes: 0
Views: 754
Reputation: 156434
I believe that the registered handlers are all called by the "trigger" function itself; so when that call returns, each of the handlers have also. Consider this example:
function checkTriggers() {
var o = {};
_.extend(o, Backbone.Events);
o.on('foo', function(){console.log('ONE');});
o.on('foo', function(){console.log('TWO');});
o.trigger('foo');
console.log('THREE');
}
The log sequence will always be ONE, TWO, THREE. So it seems like you can simply perform your action after the call to "trigger". However, if any callback functions use a delay (e.g. with "setTimeout") then you've got a different problem.
Upvotes: 1