A very common practice in Java when you want to persist or save the current state of your application to a physical file is known as serialization or serializing an object. What this basically means is that you are taking the machine code that represents an object in your application, and converting it to a format that can be written to a file.
The benefits of serialization is that you maintain the actual instance of the object–not just a data representation of the object. When you read the serialized file back in to the application, it reads in as a native object to that application.
Traditionally in web applications, we accommodate this need by writing entries in a database. Even when it comes down to session information. For the most part, this is fine, works great and I would recommend it for the majority of use cases. However, the serialization method described above could be used as a more efficient means to storing the state of a rich Internet application. Such a method is possible in ActionScript to use in your Flex application.
The high level process is:
- Represent your data model as an ActionScript object
- Serialize your data model into the same AMF format that is used to communicate with middle-tiers like ColdFusion
- Send the raw AMF message packet to ColdFusion in a RemoteObject call, or similar
- ColdFusion writes the message packet to a local file on the server
Naturally, you would create this functionality in a series of classes and CFCs. In ActionScript, you will be using the Flash ByteArray class. This class converts any ActionScript object into a ByteArray which is basically serializing the object.
var bytes:ByteArray = new ByteArray(); bytes.writeObject(myDataModel);
From this point, bytes can now be sent to ColdFusion to be written to a file. If you are using ColdFusion, you will want your service function to be expecting an arguments of type=”Binary”. From there, ColdFusion can encode the Binary into a writable format like Base64. You migh have a function that looks something like the following:
`
`
It is really that easy. Once you call the save function you will notice that file now exists on the server. If you were to open it, it would just be a series of what appears to be random data.
To read that data back into a usable object in Flex, you will need to setup a service call to ColdFusion to return the binary data from the file.
On the ColdFusion side, the code would look something like this: `
`
And your ActionScript RemoteObject handler would probably have the following:
private function myRO_handler(event:ResultEvent):void { var
myDataModel:Object = ByteArray(event.result).readObject(); }
Obviously, the above code is very procedural still since the functionality is not encapsulated within well-defined functions. It is only meant to serve as a basis for understanding the concept. For your convenience, and quite frankly for mine as well, I have created an ActionScript class as well as a set of ColdFusion components to encapsulate all this functionality. You may download the files below.
The files below are provided as is without any guarantee of support or documentation: