Created
March 12, 2012 09:35
-
-
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)
This file contains hidden or 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 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