CSimpleObservable - An Implementation of the Observer pattern in JavaScript
Introduction
Observers and Observables are a basic pattern in event-based programming.
CSimpleObservable provides a reuseable means of incorporating Observers
and Observables into your Web Applications.
Whenever a CSimpleObservable wishes to notify its observers
of a change, it will call its notify() method.
notify()
in turn will invoke a special method named observe on each
of the registered observer objects.
Script
CSimpleObservable.js
implements
a JavaScript Object CSimpleObservable which is used to
manage the Observer - Observable relationship. It uses
CList,
CCallWrapper and
inheritFrom.
- Constructor
-
- CSimpleObservable(Boolean aIsAsync)
-
Constructs an instance of a
CSimpleObservableobject.aIsAsyncspecifies whether callbacks to observers will be done via asetTimeout()or by a direct function call.Synchronous observerables are useful when observers must be notified of each distinct change using the observer's current state. Asynchronous observables are useful when only the notification is important and the exact state of the observable at the time is not required.
CSimpleObservableis intended to be inherited by other classes which wish to act as observers. SeeinheritFromfor possible means of inheritingCSimpleObservable.
- Properties
-
- CList mObservers
-
mObserversis a CList which is used to maintain the list of observer objects to be notified. - Boolean mIsAsync
-
mIsAsyncdetermines how theCSimpleObservableperforms notifications. WhenmIsAsyncis false, it performs a direct call to the observer'sobservemethod while whenmIsAsyncis true it usessetTimeoutto perform the call.
- Methods
-
- notify(Object aValue)
-
notify()is called by aCSimpleObservablewhenever it wishes to notify its observers of a change in it's state.notify()takes a single argument which can be anything theCSimpleObservableand the observer agree upon.notifywill call theobservemethod on each of the registered observers while passing theaValueargument to each. - addObserver(Object aObserver)
-
addObserver()adds an observer to be notified. The only requirement for an object to act as an observer is that it implement a method namedobserve. - removeObserver(Object aObserver)
-
addObserver()removes an observer.
Example 1
This example illustrates creating a class derived from CSimpleObservable,
attaching an observer to receive notifications, then removing the observer.
<script type="text/javascript" src="CList.js"></script>
<script type="text/javascript" src="CCallWrapper.js"></script>
<script type="text/javascript" src="inheritFrom.js"></script>
<script type="text/javascript" src="CSimpleObservable.js"></script>
<script type="text/javascript">
// create a simple synchronous observable
function CFooSync(aValue)
{
this.mValue = aValue;
// inherit from CSimpleObservable to make this
// object an Observable
inheritFrom(this, new CSimpleObservable());
}
CFooSync.prototype.get = function()
{
return this.mValue;
}
CFooSync.prototype.set = function(aValue)
{
this.mValue = aValue;
this.notify(this);
}
function doExample1()
{
// create the observable
var foo = new CFooSync('initial value');
// create the observer
var onUpdate = function (aValue)
{
alert('Observer says I received message ' + aValue.get());
}
// create an "observer" object with a single
// "observe" method.
var observer = { observe: onUpdate };
// add the observer to the observable
foo.addObserver( observer );
foo.set('bar');
// remove the observer
foo.removeObserver(observer);
foo.set('fubar');
}
</script>
Example 2
Example 2 illustrates the differences between Synchronous and Asynchronous Observables.
