Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple bind/trigger mixin for javascript object
# 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?
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