Last active
December 16, 2015 08:48
-
-
Save rikukissa/5408056 to your computer and use it in GitHub Desktop.
on & trigger methods for classes
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 EventEmitter | |
constructor: -> | |
@events = {} | |
trigger: (keys, args...) -> | |
for key in keys.split(' ') | |
if @events[key]? | |
func.apply @, args for func in @events[key] | |
@ | |
on: (keys, func) -> | |
for key in keys.split(' ') | |
@events[key] = [] if not @events[key] | |
@events[key].push func | |
@ | |
# Example class | |
class User extends EventEmitter | |
constructor: -> | |
super | |
@id = Date.now() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment