DekGenius.com
[ Team LiB ] Previous Section Next Section

28.5 Using LoadVars and XML

The third way you can modify the scheduler application is to use server-side scripts that read and write to a file. The ColdFusion versions of these scripts are provided here in this example, but if you need to use another technology, it is not too difficult. You need two scripts: one to write data and one to read the data. The write script should take two parameters submitted via HTTP POST: the name of the file to which to write and the data to be written. Here is the ColdFusion example that you should name LoadVarsSharedObjectSave.cfm:

<cffile action="write" 
   file="#GetDirectoryFromPath(GetBaseTemplatePath(  ))##FORM.name#.so" 
   output="#FORM.data#" nameconflict="overwrite">

The other script should read the data from the file and return it to Flash. This script should accept a single parameter submitted via HTTP GET: the name of the file from which to read. Here is the ColdFusion example. You should name this file LoadVarsSharedObjectRead.cfm.

<cfsetting enablecfoutputonly="yes">
<cffile action="read" 
     file="#GetDirectoryFromPath(GetBaseTemplatePath(  ))##URL.name#.so" 
     variable="data">
<cfoutput>#data#</cfoutput>

Finally, you should make a few changes to the client-side ActionScript code in the Schedule component. The relevant snippet of code from that component is shown here with changes in bold:

#initclip

// The serializeObject(  ) and deserializeObject(  ) functions are the exact same code
// as the serializeObject(  ) and deserializeObject(  ) methods of the FauxSharedObject 
// class from Chapter 27. See Chapter 27 for more information on these functions.
_global.serializeObject = function (obj) {
  var sb = "<obj>";
  if (obj instanceof Array) {
    sb = "<obj type=\"array\">";
  }
  for (var i in obj) {
    sb += "<index name=\"" + i + "\"";
    if (obj[i] instanceof Object) {
      sb += ">";
      sb += serializeObject(obj[i]);
      sb += "</index>";
    } else {
      sb += " val=\"" + obj[i] + "\" />";
    }
  }
  sb += "</obj>";
   return sb;
};

_global.deserializeObject = function (source) {
  XML.prototype.ignoreWhite = true;
  var sXML = new XML(source);
  var cn = sXML.firstChild.childNodes;
  var items = new Object(  );
  if (sXML.firstChild.attributes.type == "array") {
    items = new Array(  );
  }
  var attribs, childCn;
  for (var i = 0; i < cn.length; i++) {
    attribs = cn[i].attributes;
    childCn = cn[i].childNodes;
    if (childCn.length > 0) {
      items[attribs.name] = deserializeObject(childCn[0]);
    } else {
      items[attribs.name] = attribs.val;
    }
    items[attribs.index] = new Object(  );
    for (var j in attribs) {
      items[attribs.index][j] = attribs[j];
    }
  }
  return items;
};

function Schedule (  ) {
  this.items = new Object(  );
  this.notifiers = new Object(  );

  // Instead of a shared object, simply create an object with a data property.
  this.so = new Object(  );
  this.so.data = new Object(  );

  // Create a flush(  ) method for the object that uses a LoadVars object to send the
  // serialized data to the LoadVarsSharedObjectSave.cfm script.
  this.so.flush = function (  ) {
    serializedStr = serializeObject(this.data);
    var sendLV = new LoadVars(  );
    sendLV.name = "myScheduleLV";
    sendLV.data = serializedStr;
    sendLV.sendAndLoad("http://localhost:8500/LoadVarsSharedObjectSave.cfm", 
                       null, "POST");
  };

  this.so.parent = this;

  // Create a load method for the object that uses an XML object to load the data
  // from the LoadVarsSharedObjectRead.cfm script.
  this.so.load = function (  ) {
    var loadXML = new XML(  );
    loadXML.ignoreWhite = true;
    loadXML.load(
           "http://localhost:8500/LoadVarsSharedObjectRead.cfm?name=myScheduleLV");
    loadXML.parent = this;

    // When the data loads, deserialize it and then call the setValues(  ) method.
    loadXML.onLoad = function (  ) {
      this.parent.data = deserializeObject(this);
      this.parent.parent.setValues(  );
    };
  };
  this.so.load(  );
  this.runNotify(  );
  this.interval = setInterval(this, "runNotify", 600000);
  this.saveInterval = setInterval(this, "save", 5000);
}

Schedule.prototype = new MovieClip(  );

// The setValues(  ) method contains code that was previously in the constructor.
Schedule.prototype.setValues = function (  ) {
  if (this.so.data.schedules != undefined) {
    for (var i in this.so.data.schedules) {
      var dt = this.so.data.schedules[i].siDate;
      this.addItem(dt);
      this.items[dt].setValues(this.so.data.schedules[i]);
    }
    this.notifiers = this.so.data.notifiers;
  }
};

// The remainder of the code stays the same.
    [ Team LiB ] Previous Section Next Section