Created
June 26, 2014 18:41
-
-
Save j0lvera/a4d2dde6a214f4836f43 to your computer and use it in GitHub Desktop.
Events Class
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 Events = { | |
_topics: {}, | |
_subUid: -1, | |
trigger: function(event, args) { | |
if (!this._topics[event]) { | |
return false; | |
} | |
var events = this._topics[event], | |
len = events ? events.length : 0; | |
while (len--) { | |
events[len].callback(event, args); | |
} | |
return this; | |
}, | |
on: function(event, callback) { | |
if (!this._topics[event]) { | |
this._topics[event] = []; | |
} | |
var token = (++this._subUid).toString(); | |
this._topics[event].push({ | |
token: token, | |
callback: callback | |
}); | |
return token; | |
}, | |
off: function(token) { | |
for (var m in this._topics) { | |
if (this._topics[m]) { | |
for (var i = 0, j = this._topics[m].length; i < j; i ++) { | |
if (this._topics[m][i].token === token) { | |
this._topics[m].splice(i, 1); | |
return token; | |
} | |
} | |
} | |
} | |
return this; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment