Last active
November 25, 2015 20:31
-
-
Save kvendrik/056c20ef0e1ccfbb9fd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var ListenersHelper = function(){ | |
| this._listeners = {}; | |
| }; | |
| ListenersHelper.prototype.on = function(event, callback){ | |
| if(typeof this._listeners[event] !== 'object'){ | |
| this._listeners[event] = []; | |
| } | |
| this._listeners[event].push(callback); | |
| }; | |
| ListenersHelper.prototype.off = function(event, callback){ | |
| var listeners = this._listeners[event], | |
| idx = listeners.indexOf(callback); | |
| if(typeof listeners === 'object' && idx !== -1){ | |
| this._listeners[event].splice(idx, 1); | |
| } | |
| }; | |
| ListenersHelper.prototype.triggerEvent = function(event, argsArr){ | |
| var listeners = this._listeners[event]; | |
| if(typeof listeners === 'object'){ | |
| for(var i = 0, l = listeners.length; i < l; i++){ | |
| listeners[i].apply(null, argsArr); | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment