You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
function BaseEvent() {}
|
|
BaseEvent.prototype = {
|
|
triggerEvent: function (eventName, args) {
|
|
if (this._cbs[eventName]) {
|
|
var callbacks = this._cbs[eventName];
|
|
for (var i = 0; i < callbacks.length; i += 1) {
|
|
callbacks[i](args);
|
|
}
|
|
}
|
|
},
|
|
addEventListener: function (eventName, callback) {
|
|
if (!this._cbs[eventName]) {
|
|
this._cbs[eventName] = [];
|
|
}
|
|
this._cbs[eventName].push(callback);
|
|
|
|
return function () {
|
|
this.removeEventListener(eventName, callback);
|
|
}.bind(this);
|
|
},
|
|
removeEventListener: function (eventName, callback) {
|
|
if (!callback) {
|
|
this._cbs[eventName] = null;
|
|
} else if (this._cbs[eventName]) {
|
|
var i = 0;
|
|
var len = this._cbs[eventName].length;
|
|
while (i < len) {
|
|
if (this._cbs[eventName][i] === callback) {
|
|
this._cbs[eventName].splice(i, 1);
|
|
i -= 1;
|
|
len -= 1;
|
|
}
|
|
i += 1;
|
|
}
|
|
if (!this._cbs[eventName].length) {
|
|
this._cbs[eventName] = null;
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
export default BaseEvent;
|