Skip to content

Instantly share code, notes, and snippets.

@sdiehl
Created May 18, 2011 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sdiehl/977881 to your computer and use it in GitHub Desktop.
Save sdiehl/977881 to your computer and use it in GitHub Desktop.
Observer Pattern in Coffeescript
class Observer
bind : (event, fn) ->
this._events ||= {}
this._events[event] ||= []
this._events[event].push(fn)
unbind: (event, fn) ->
@_events ||= {}
if @_events[event]
@_events[event].splice(@_events[event].indexOf(fn), 1)
trigger: (event, args...) ->
@_events ||= {}
if @_events[event]
for callback in @_events[event]
callback.apply(this, args)
exports.Observer = Observer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment