Spry

Region observer notifications

Spry supports an observer mechanism that allows a developer to register an object or function to receive a notification whenever the state of a region changes. This mechanism is almost identical to what is used for data sets with the following exceptions:

  • Adding and removing region observers is done through the Spry.Data.Region.addObserver() and Spry.Data.Region.removeObserver global namespaced functions. This practice is different from data sets because data set observers call addObserver() and removeObserver() methods that are on the data set object. The use of global namespaced functions allows a developer to register an observer before the document's onload event starts, and before Spry creates the JavaScript object that represents the region. Regions use addObserver and removeObserver functions because the developer might want to register observers before the JavaScript region object actually exists.

  • Both addObserver() and removeObserver() require an ID to identify which region the developer wants to observe. For this reason, regions that developers want to observe must have an id attribute defined on their region container node.

Objects as region observers

To receive notifications, an object must define a method for each notification it is interested in receiving, and then register itself as an observer on the region.

The following example shows an object being registered as an observer on a dynamic region:

<script>
...
// Create an observer object and define the methods to receive the notifications
// it wants.
myObserver = new Object;
myObserver.onPostUpdate = function(notifier, data)
{
	alert("onPostUpdate called for " + data.regionID);
};
...
// Call addObserver() to register the object as an observer.
Spry.Data.Region.addObserver("employeeListRegion", myObserver);
...
// You can unregister your object so it stops recieving notifications
// with a call to removeObserver().
Spry.Data.Region.removeObserver("employeeListRegion", myObserver);
...
</script>
...
<ul id="employeeListRegion" spry:region="dsEmployees">
...
</ul>

The first argument for each notification method is the object that is sending the notification. For region observers, this is not the region object. The second argument is an object that contains a regionID property that identifies the region that triggered the notification.

To stop an object from receiving notifications, the object must be removed from the list of observers with a call to removeObserver().

Functions as region observers

Functions can also be registered as observers. The main difference between object and function observers is that an object is only notified for the notification methods it defines, whereas a function observer is called for every type of event notification.

The following example shows a function being registered as an observer on a dynamic region:

<script>
...
function myRegionCallback(notificationState, notifier, data)
{
	if (notificationType == "onPreUpdate")
		alert(regionID + " is starting an update!");
	else if (notificationType == "onPostUpdate")
		alert(regionID + " is done updating!");
}
...
// Call addObserver() to register your function as an observer.
Spry.Data.Region.addObserver("employeeListRegion", MyRegionCallback);
...
// You can unregister your callback function so it stops recieving notifications
// with a call to removeObserver().
Spry.Data.Region.removeObserver("employeeListRegion", MyRegionCallback);
...
</script>
...
<ul id="employeeListRegion" spry:region="dsEmployees">
...
</ul>

A function observer is registered with the same call to addObserver().

When the function is called, the first argument passed into it is the notification type. This is a string that is the name of the notification. The second argument is the notifier, which in this case is not the region object. The third argument is a data object that has a regionID property that tells us what region triggered the notification.

To stop a function observer from receiving notifications, it must be removed from the list of observers with a call to removeObserver().

The following table describes the current set of supported notifications.

Region notification type

Description

onLoadingData

One or more of the region's bound data sets is loading its data.

onPreUpdate

All of the region's bound data sets have loaded successfully. The region is about to regenerate its code.

onPostUpdate

The region has regenerated its code and inserted it into the document.

onError

An error occurred while loading data.