Skip to content

Instantly share code, notes, and snippets.

@deciob
Created March 12, 2012 09:35
Show Gist options
  • Save deciob/2020925 to your computer and use it in GitHub Desktop.
Save deciob/2020925 to your computer and use it in GitHub Desktop.
Simple event-observer class inspired from the example on: http://api.jquery.com/jQuery.Callbacks/ (needs JQuery 1.7)
class Observer
actions: {}
init: (id) ->
action = id && @actions[ id ]
callbacks = $.Callbacks()
if not action
action =
notify: callbacks.fireWith,
register: callbacks.add,
unregister: callbacks.remove
@actions[ id ] = action
register: (id, callback) ->
if not @actions[id]
@init(id)
@actions[ id ].register(callback)
notify: (id, context=window, data=false) ->
if @actions[id]
if data
@actions[id].notify(context, data)
else
@actions[id].notify(context)
module.exports = Observer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment