Skip to content

Instantly share code, notes, and snippets.

@tomz
Created April 11, 2014 16:27
Show Gist options
  • Save tomz/10482076 to your computer and use it in GitHub Desktop.
Save tomz/10482076 to your computer and use it in GitHub Desktop.
(->
slice = Array::slice
@events =
events: {}
on: (topic, handler, context) ->
context = this unless context?
@events[topic] = @events[topic] or []
@events[topic].push
handler: handler
context: context
trigger: (topic) ->
return unless @events[topic]?
args = slice.apply(arguments_, 1)
i = 0
l = @events[topic].length
event = undefined
while i < l
event = @events[topic][i]
event.handler.apply event.context, args
i++
).call this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment