/**
* Quick and dirty, convert the object into a byte stream, save it off.
* For object with depth, be sure to cast them back before using.
* public function saveCart():void{
* ClientSettings.write(“cart”,_cartItems); //cartitems is an ArrayCollection containing many cart item objects.
* }
*
* Read the object back in with:
* var tmpAL:ArrayCollection=obj as ArrayCollection;
* if(_cartItems==null){
* _cartItems=new ArrayCollection();
* }else{
* for each (var item:Object in tmpAL ){
* var cItem:CartItem=new CartItem(item);
* _cartItems.addItem(cItem);
* }
* }
*
*/
package com.ackdev.utils.Serialize
{
public class ClientSettings
{
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
import flash.utils.ByteArray;
private static var mySo:SharedObject = SharedObject.getLocal(“application-name”);
public function ClientSettings()
{
}
public static function write(name:String,obj:Object):void{
//create a bytearray
var bytes:ByteArray = new ByteArray();
//write an object into the bytearray
bytes.writeObject(obj);
mySo.data[name]=bytes;
var flushStatus:String = null;
try {
flushStatus = mySo.flush(10000);
} catch (error:Error) {
trace(error.message);
}
if (flushStatus != null) {
switch (flushStatus) {
case SharedObjectFlushStatus.PENDING:
break;
case SharedObjectFlushStatus.FLUSHED:
break;
}
}
}
public static function read(name:String):Object{
//create an object and deserialize an object from a bytearray
var myObject:Object =null;
var bytes:ByteArray = mySo.data[name] as ByteArray;
if(bytes!=null && bytes.length!=0)
myObject= bytes.readObject();
return myObject;
}
public static function clear(name:String):void{
delete mySo.data[name];
mySo.flush(10000);
}
}
}
Tags: as2, as3, deep cloning, Flex