Source code


This script provides a function Observable to make an object call observers when events occur.

Observable should be called on the prototype of a constructor, but it can also be called on individual objects. The first option is preferable, because it is faster and only attaches the methods to the prototype and not to every single instance.

Observable attaches the methods addObserver , removeObserver , removeObservers and notify to the object.

On the first call to addObserver the object receives a property _observableObservers .



Makes an object observable.

obj (object) : the object to make observable

Add an observer (a function) to to be called when event occurs.

event (string) : the event that is observed
observer (function) : the function that should be called on the event

Notify all observers of event.

event (string) : the event that occurs
arguments (any) : arguments that will be passed to the observers

Remove an observer.

event (string) : the event that the observer observes
observer (function) : the observer to be removed

Remove observers. If an event is passed, only observers of that event will be removed. Otherwise all observers will be removed.

event (undefined,string) : the event for which observers should be removed


2007-08-17 : Added removeObservers.
2007-06-13 : Created.

API Documentation for version