Example Number 4.3

Example Name: RecordSetDebug.as

Description: Full implementation of the RecordSetDebug functionality

Language: ActionScript 1.0

Version: 1.0.0

Code:

/////////////////////////////////////////
// RecordSet.debug
// Purpose: Trace all changes to the RecordSet 
//          as well as the RecordSet properties
/////////////////////////////////////////

// Main public method to debug the RecordSet
// Call it like this: myRecordset_rs.debug(true);
// Turn it off like this: myRecordset_rs.debug(false);

RecordSet.prototype.debug = function(enabled) {
  if(enabled) {
    if(!this.debugObject)
      this.debugObject = new RecordSetDebugObject(this);
  }else{
    this.debugObject.modelChanged = null;
  this.debugObject = null;
  }
}

// Create a new object that debugs a RecordSet that is passed to it
function RecordSetDebugObject(rs) { 
  this.init(rs);
}

// Class initialization, including the addView() method
RecordSetDebugObject.prototype.init = function(rs) {
  this.rs = rs;
  this.rs.addView(this);
}

// This method is now called whenever a change is made in a RecordSet
// It will simply display the event and start/end rows affected, as 
// well as the RecordSet.showData information
RecordSetDebugObject.prototype.modelChanged = function(info) {
  trace("");
  trace("--Recordset event occurred--");
  trace("Event: " + info.event);
  switch(info.event) {
    case("sort"):
      trace("The RecordSet has been sorted.");
      break; 
    case("updateAll"):
      trace("The RecordSet has changed in some way")
      break;
    case("addRows"):
      trace("firstRow:" + info.firstRow);
      trace("lastRow:" + info.lastRow);
      trace("Row numbers " + info.firstRow + 
        " through "+ info.lastRow +" have been added.");
      break;
    case("updateRows"):
      trace("firstRow:" + info.firstRow);
      trace("lastRow:" + info.lastRow);
      trace("Row numbers " + info.firstRow + " through " + 
        info.lastRow +" have been changed.");
      break;
    case("deleteRows"):
      trace("firstRow:" + info.firstRow);
      trace("lastRow:" + info.lastRow);
      trace("Row numbers " + info.firstRow + " through " + 
         info.lastRow +" have been deleted.");
      break;
    case("allRows"):
      trace("All records have arrived from the server.");
      break;
    case("fetchrows"):
      trace("firstRow:" + info.firstRow);
      trace("lastRow:" + info.lastRow);
      trace("Row numbers " + info.firstRow + " through " + 
        info.lastRow +" have been requested from the server.");
      break;
  }
  this.rs.showData(); // call the showData method within the debug
  trace("--End recordset event--");
}

/////////////////////////////////////////
// Recordset.showData
// Purpose: trace the contents of the RecordSet object to the 
//          trace window
/////////////////////////////////////////

RecordSet.prototype.showData = function() {
  var fields = this.getColumnNames();
  var i,j,tempfield="",temprow="",temprec="";
  trace("  --Recordset Properties--");
  trace("Recordset length: " + this.getLength());
  trace("Fields: " + fields);
  trace("Begin records...");
  for (var i = 0; i<this.getLength(); i++) {
    temprec = this.getItemAt(i);
    for(var j=0; j < fields.length; j++){
      tempfield = fields[j];
      temprow += tempfield + ': "' + temprec[tempfield] + '"; ';
    }
    trace(temprow);
    temprow="";
  }
  trace("End records...");
  trace("  --End Recordset Properties--");

Download code text

Download chapter example files