123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 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;
|