RecordSet.addView( )

RecordSet.addView( ) MethodFlash 6

monitors RecordSet activity by watching certain events
myRecordSet.addView(object)

Arguments

object

An object with a method named modelChanged( ) that is notified of changes to the recordset.

Description

The addView( ) method allows you to specify an object to be notified whenever changes occur to a RecordSet object. This allows you to perform certain actions in response to those changes. For example, if you want to implement a recordset logging feature, you can use an object that displays information about the recordset in the Output window whenever the recordset changes.

Typically addView( ) is used during debugging to verify that certain events happened. Chapter 4 discusses the method in detail, and Example 4-3 creates a RecordSetDebug.as file containing methods to view recordsets for debugging purposes.

Table 15-3 shows the event information sent to the object specified in the addView( ) method. It also indicates which operations generate each event.

Table 15-3. Events tracked in the addView( ) method

Event

Information object returned

Occurs when

addItem( ) and addItemAt( ) methods

{event:"addRows", firstRow:n, lastRow:nn}

Row numbers between n and nn are added

onResult( ) method of responder object

{event:"allRows"}

The recordset is fully populated from the server

removeAll( ), removeItemAt( )

{event:"deleteRows", firstRow:n, lastRow:nn}

Row numbers between n and nn are deleted

onResult( ) method of responder object

{event:"fetchrows", firstRow:n, lastRow:nn}

Row numbers between n and nn are requested from the server, but have not yet arrived

sort( ) and sortItemsBy( ) methods

{event: "sort"}

A recordset is sorted

Any change in the RecordSet

{event:"updateAll"}

A recordset changes in any way

replaceItemAt( ), setField( )

{event:"updateRows", firstRow:n, lastRow:nn}

Row numbers between n and nn change in any way

Example

First, create an ActionScript object that defines a modelChanged( ) method, like this:

var myObject = new Object( );
myObject.prototype.modelChanged = function (myInformationObject) {
  trace(myInformationObject.event);
};

Then call addView( ), passing myObject as a parameter:

myRecordset_rs.addView(myObject);

This causes myObject's modelChanged( ) method to be invoked whenever the contents of myRecordset_rs change. When an event triggers the modelChanged( ) method, it receives an information object (myInformationObject) as a parameter. This information object contains an event property, indicating the type of change that occurred to the recordset, as shown in Table 15-3. Some objects passed to modelChanged( ) also contain firstRow and lastRow properties indicating the range or record numbers affected.

Here is a fleshed-out example that displays a message in the Output window whenever the recordset is sorted:

#include "RecordSet.as"
var myRecordset_rs = new RecordSet(["First","Last","Email"]);
// Create the generic object
var myObject = new Object( );
// Add a modelChanged( ) event handler the object
myObject.modelChanged = function (myInformationObject) {
  if (myInformationObject.event == "sort") {
    trace("RecordSet was sorted");
  }
};
// Call addView( ) to set up the callback function (myObject.modelChanged)
myRecordset_rs.addView(myObject);
myRecordset_rs.addItem({First:"Tom",Last:"Muck",Email:"tom@tom-muck.com"});
myRecordset_rs.addItem({First:"Jack",Last:"Splat",Email:"jack@tom-muck.com"});

// Sort the recordset, triggering the modelChanged( ) method
myRecordset_rs.sortItemsBy("First");

At this point, the Output window should display "RecordSet was sorted."

See Also

Chapter 4 (especially Example 4-3)



    Part III: Advanced Flash Remoting