Created
June 14, 2013 02:25
-
-
Save jaeschrich/5779036 to your computer and use it in GitHub Desktop.
CoffeScript event emitter
This file contains 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
class Emitter | |
constructor: -> | |
@events = {} | |
on: (e, cb)-> | |
@events[e] = [] unless @events[e] | |
@events[e].push cb | |
this | |
off: (e, cb)-> | |
unless cb | |
@events[e] = [] | |
else | |
index = @events[e].indexOf cb | |
@events[e].splice index, 1 | |
emit: -> | |
args = Array.prototype.slice.call arguments | |
e = args.shift() | |
self = this | |
@events[e].forEach (cb)-> | |
cb.apply self, args | |
this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment