Spry

Work with observer notifications

Observer notifications overview

The XML data set supports an observer mechanism that allows an object or callback function to receive event notifications.

Notification

Description

onPreLoad

The data set is about to send a request for data. If the data set depends on other data sets, this event notification will not send until they are all loaded successfully.

onPostLoad

The request for data was successful. The data is accessible.

onLoadError

An error occurred while requesting the data.

onDataChanged

The data in the data set has been modified.

onPreSort

The data in the data set is about to be sorted.

onPostSort

The data in the data set has been sorted.

onCurrentRowChanged

The data set's notion of the current row has changed.

Objects as 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 data set. For example, an object interested in onDataChanged notifications must define an onDataChanged() method and then call addObserver().

var dsPhotos = new Spry.Data.XMLDataSet("/photos.php?galleryid=2000", "/gallery/photos/photo");
...
var myObserver = new Object;
myObserver.onDataChanged = function(dataSet, data)
{
	alert("onDataChanged called!");
};
dsPhotos.addObserver(myObserver);

The first argument for each notification method is the object that is sending the notification. For data set observers, this is always the data set object. The second argument is either undefined, or an object that depends on the type of notification.

Notification

Data passed into notification

onPreLoad

undefined

onPostLoad

undefined

onLoadError

The Spry.Utils.loadURL.Request object that was used when making the request

onDataChanged

undefined

onPreSort

Object with the following properties:

oldSortColumns: An array of columns used during the last sort.

oldSortOrder: The sort order used during the last sort.

newSortColumns: The array of columns about to be used for the sort.

newSortOrder: The sort order about to be used for the sort.

onPostSort

Object with the following properties:

oldSortColumns: An array of the columns used in the previous sort.

oldSortOrder: The sort order used during the previous sort.

newSortColumns: The array of columns used for the sort.

newSortOrder: The sort order used for the sort.

onCurrentRowChanged

Object with the following properties:

oldRowID: The ds_RowID of the last current row.

newRowID: The ds_RowID of the current row.

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

var dsPhotos = new Spry.Data.XMLDataSet("/photos.php?galleryid=2000", "/gallery/photos/photo");
...
dsPhotos.removeObserver(myObserver);

Functions as 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.

var dsPhotos = new Spry.Data.XMLDataSet("/photos.php?galleryid=2000", "/gallery/photos/photo");
...
function myObserverFunc(notificationType, dataSet, data)
{
	if (notificationType == "onDataChanged")
		alert("onDataChanged called!";
	else if (notificationType == "onPostSort")
		alert("onPostSort called!";
};
dsPhotos.addObserver(myObserverFunc);

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

When the function is called, the first argument to be 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 the data set, and the third argument is the data for the notification.

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

var dsPhotos = new Spry.Data.XMLDataSet("/photos.php?galleryid=2000", "/gallery/photos/photo");
...
dsPhotos.removeObserver(myObserverFunc);