| function BaseEvent(){} |
| BaseEvent.prototype = { |
| triggerEvent: function (eventName, args) { |
| if (this._cbs[eventName]) { |
| var len = this._cbs[eventName].length; |
| for (var i = 0; i < len; i++){ |
| this._cbs[eventName][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, 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; |
| } |
| } |
| } |
| }; |