Light12306/Web12306/js/platform/EventObject.js

39 lines
911 B
JavaScript
Raw Normal View History

2014-03-13 15:06:01 +08:00
define(function() {
function EventObject() {
var e = this;
this.handles = {};
this.on = function (name, callback) {
var handleQueue = e.handles[name] || [];
e.handles[name] = handleQueue;
handleQueue.push(callback);
};
this.off = function (name, callback) {
var handleQueue = e.handles[name] || [];
if (callback) {
for (var i = handleQueue.length - 1; i >= 0; i++) {
if (handleQueue[i] == callback)
handleQueue.splice(i, 1);
}
} else {
handleQueue.length = 0;
}
e.handles[name] = handleQueue;
};
this.fireEvent = function (name, args) {
var handleQueue = e.handles[name];
if (!handleQueue) return;
for (var i in handleQueue) {
var handle = handleQueue[i];
if (!handle) continue;
if (handle.apply(this, args) === false)
break;
}
};
return this;
}
return EventObject;
});