Created
October 22, 2012 20:48
-
-
Save robhurring/3934126 to your computer and use it in GitHub Desktop.
Simple bind/trigger mixin for javascript object
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
# this: https://gist.github.com/464257 in coffeescript, without the global stuff... | |
class EventsDispatcher | |
callbacks: {} | |
bind: (event_name, callback) -> | |
@callbacks[event_name] ||= [] | |
@callbacks[event_name].push callback | |
@ | |
trigger: (event_name, data) -> | |
@dispatch event_name, data | |
@ | |
dispatch: (event_name, data) -> | |
chain = @callbacks[event_name] | |
callback data for callback in chain if chain? |
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 Connection extends EventsDispatcher | |
connect: -> | |
@trigger 'connected' | |
something = new Connection() | |
something.bind 'connected', -> | |
console.log 'i connected!' | |
something.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment