RecordSet.addView( )

RecordSet.addView( ) MethodFlash 6

monitors RecordSet activity by watching certain events



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


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 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


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


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


A recordset changes in any way

replaceItemAt( ), setField( )

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

Row numbers between n and nn change in any way


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

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

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


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 ""
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)

// Sort the recordset, triggering the modelChanged( ) method

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

See Also

Chapter 4 (especially Example 4-3)

    Part III: Advanced Flash Remoting