Notifier

Description

The Spry.Utils.Notifier class provides the base functionality necessary to maintain a list of observers and send notifications to them.

The Notifier class does not define any notification messages, so it is up to the developer to define the notifications sent via the Notifier. Notifications consist of a string (the name of the notification) and some optional data which the developer may want to pass from the point of notification to any observers.

Observers can be functions (callbacks) or objects, and can be added or removed dynamically from the notifier. See addObserver for more details.

Developers can create an instance of the Spry.Utils.Notifier class directly, or use it as a base class for one of their own classes.

File

SpryData.js

Inheritance

N/A

Notifier constructor

Description

Spry.Utils.Notifier is the constructor function for the Notifier. The constructor does not take any arguments.

Format

Spry.Utils.Notifier()

Example

The following example shows how to invoke an instance of a notifier directly:

var n = new Spry.Utils.Notifier();

The following example shows how to use the Notifier as a base class for your own class:

// Define a constructor for MyClass:

function MyClass()
{
  ...

  Spry.Utils.Notifier.call(this);

  ...
}

// Make sure the MyClass prototype object has all
// of the Notifier methods on it.

MyClass.prototype = new Spry.Utils.Notifier();

// Reset the constructor on MyClass.

MyClass.prototype.constructor = MyClass;

addObserver

Description

addObserver adds an observer function or object to the list of observers to notify.

Observer functions should have an interface like this:

function MyObserverFunc(notificationType, notifier, data)
{

  ...

}

The notificationType is a string that is the name of the notification. The notifier is the object instance of the notifier that was used to fire off the notification. The data argument is any data that may have been passed along with the notification. If no data is passed at the time of notification, data will be undefined. See notifiyObservers for more details.

Observer objects should have method properties on them with the same name as the notifications they are interested in recieving. The interface for these methods is as follows:

var observer = new Object;

// We are interested in the onComplete notification,
// so define it on our observer object:

observer.onComplete = function(notifier, data)
{
  ...
};

As with the function observer, the notifier argument is the instance of the notifier that was used to fire off the notification, and the data is the optional data that was passed along with the notification.

The big difference between the a function observer and an object observer is that a function observer will get called for *all* notifications made with the notifier it is observing, where an object observer will only get notified for the notifications it has methods defined for.

Format

addObserver(observer)

Example

// Create a notifier:

var n = new Spry.Utils.Notifier();

// Here's an example of adding an object as an observer:

var obj = new Object;

obj.onComplete = function(notifier, data)
{
	alert("onComplete recieved!");
}

n.addObserver(obj);

// Here's an example of adding a function as an observer:

function MyObserverFunc(notificationType, notifier, data)
{
  // Thow an alert for *every* notification!

  alert(notificationType);
}

n.addObserver(MyObserverFunc);

disableNotifications

Description

Disables the notification mechanism of the notifier. Any calls made with notifyObservers will be ignored until enableNotifications is called.

The disableNotifications method uses a counter internally which allows multiple calls to disableNotification to be nested within the developers code. If multiple calls to disableNotifications are made, the same number of enableNotifications calls must be made before notifications are re-enabled.

Format

 disableNotifications()

Example

var n = new Spry.Utils.Notifier();

// Turn off any notifications, until we
// are ready.

n.disableNotifications();

enableNotifications

Description

Enables the internal notification mechanism of the notifier. When enabled, this allows the notifyObservers method of the notifier to dispatch notifications to all of its observers. The notification mechanism of the notifier is enabled by default so a call to enableNotifications is only necessary if a call to disableNotifications was made.

The enableNotifications method decrements the internal counter used by disableNotifications, so the number of calls to enableNotifications must match the number of calls made to disableNotifications before notifications are truly enabled.

Format

enableNotifications()

Example

var n = new Spry.Utils.Notifier();

// Turn off any notifications, until we
// are ready.

n.disableNotifications();


...

// Turn on notifications because we're ready!

n.enableNotifications();

notifyObservers

Description

Sends a notification to all interested observers registered with the notifier.

Format

notifyObservers(notificationType, data)

notificationType is a string that is the name of the notification. For example "onComplete". The data argument is optional and is provided as a means for passing data from the point of notification to all interested observers. If not used, observers will get an undefined value for data if they try to access it.

Example

var n = new Spry.Utils.Notifier();

...


// Fire off a notification for any observers interested in
// profiling information. Pass the current time as data.


var d = new Date();

n.notifyObservers("onProfileComplete", d.getTime());

// Fire off a notification that we are complete now. The onComplete
// notification doesn't require any data to be passed to the observers.

n.notifyObservers("onComplete");

removeObserver

Description

Removes an observer from the notifier's list of observers.

Format

removeObserver(observer)

observer is the observer function reference or object to remove from the list.

Example

// Create a notifier:

var n = new Spry.Utils.Notifier();

// Here's an example of adding an object as an observer:

var obj = new Object;

obj.onComplete = function(notifier, data)
{
	alert("onComplete recieved!");
}

n.addObserver(obj);

// Here's an example of adding a function as an observer:

function MyObserverFunc(notificationType, notifier, data)
{
  // Thow an alert for *every* notification!

  alert(notificationType);
}

n.addObserver(MyObserverFunc);

...

// Remove the observers we added:

n.removeObserver(obj);
n.removeObserver(MyObserverFunc);

Copyright © 2007. Adobe Systems Incorporated.
All rights reserved.