Comparing two custom ActionScript classes

I have a custom employee class in my Flex that has name, badge number, phone number, etc, etc. In the form to update it, I want to only actually do to the database if the record was changed. The save function takes the existing record and the new record with changes and shoves it to ColdFusion, who generates an email with the before and after states.

I could have disabled the "save" button if the record isn't changed, only giving the user a "close" button. But there are a lot of fields on the form and more are being added in the future. I don't want some giant function that determines if the before and after employee objects are different, comparing one attribute at a time.

So I went to the kitchen cabinet and got some serial.

Thanks to Snipplr, I found just the code I needed. I added a serialize() function to my custom class.

package Classes
{
   import flash.utils.ByteArray;   
   import mx.utils.Base64Encoder;
   
   [RemoteClass(alias="path.to.cfc")]

   [Bindable]
   public class Employee
   {

      public var EmployeeBadgeID:String = "";
      public var AspectExtn:String = "";
      public var EmployeeFirstName:String = "";
      public var EmployeeLastName:String = "";
      public var EmployeeMiddleName:String = "";
      public var EmpPosition:String = "";
      public var WorkType:String = "";
      public var Start_Date:Date = null;
      public var End_Date:Date = null;
      public var AssetTag:String = "";

      public function Employee(){
      }
      
      public function serialize():String{
         var bytes:ByteArray = new ByteArray()
         bytes.writeObject(this);
         bytes.position = 0;
         var be:Base64Encoder = new Base64Encoder();
         be.encode(bytes.readUTFBytes(bytes.length));
         return be.drain();
      }

   }
}

...that serializing code, I don't understand, but I don't have to. And now my doSave() function can choose to fire off the save or not. With this in mind, I could now dis/enable the save button when things change, but I think I'd rather not.

private function doSave():void{
   var after:String = editEmp.serialize();
   var before:String = _existingEmp.serialize();
   if( before == after ){
      close(false);
      return;
   }
   
   roEmp.saveEmployee(this.editEmp, this._existingEmp);
   
   close(true);
}

Comments
EDIT:

This doesn't work at all :)
# Posted By Murloc Oracle | 10/14/08 10:25 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner