/**
* Module Dependencies
*/
var events = require('event');
/**
* Exports
*/
/**
* Registers a event listener.
*
* @param {String} name
* @param {String} module
* @param {Function} cb
* @return {View}
*/
exports.on = function(name, module, cb) {
// cb can be passed as
// the second or third argument
if (arguments.length === 2) {
cb = module;
module = null;
}
// if a module is provided
// pass in a special callback
// function that checks the
// module
if (module) {
events.prototype.on.call(this, name, function() {
if (this.event.target.module() === module) {
cb.apply(this, arguments);
}
});
} else {
events.prototype.on.call(this, name, cb);
}
return this;
};
/**
* Fires an event on a view.
*
* @param {String} name
* @return {View}
*/
exports.fire = function(name) {
var _event = this.event;
var event = {
target: this,
propagate: true,
stopPropagation: function(){ this.propagate = false; }
};
propagate(this, arguments, event);
// COMPLEX:
// If an earlier event object was
// cached, restore the the event
// back onto the view. If there
// wasn't an earlier event, make
// sure the `event` key has been
// deleted off the view.
if (_event) this.event = _event;
else delete this.event;
// Allow chaining
return this;
};
function propagate(view, args, event) {
if (!view || !event.propagate) return;
view.event = event;
events.prototype.fire.apply(view, args);
propagate(view.parent, args, event);
}
exports.fireStatic = events.prototype.fire;
exports.off = events.prototype.off; |