Today the Flex tip will talk about the usage of the local SharedObject object and how to implement it to store simple data objects.
When you create a SharedObject obejct the Flash Player also creates an empty *.sol file that stores the SharedObject data limited up to 100 KB per domain. If you try to saving bigger data the user must to authorize this process through a Local Storage dialog box. The supported types are: Number, String, Boolean, XML, Date, Array and Objects. You can also store typed ActionScript instances by callling the flash.net.registerClassAlias() function in order to preserve the class type of an object when the object is encoded in AMF.
Use the SharedObject.getLocal() method for create a SharedObject object:
SharedObject.getLocal("chooseSomeName"): SharedObject |
For instance:
var sharedObject:SharedObject; sharedObject = SharedObject.getLocal("MyTextInformation"); |
You can also create multiple SharedObjects for the same Flex application:
sharedObject1 = SharedObject.getLocal("UserInformation"); sharedObject2 = SharedObject.getLocal("TravelInformation"); sharedObject3 = SharedObject.getLocal("AgreementInformation"); |
The code above will create 3 files in the Flex folder:
UserInformation.sol
TravelInformation.sol
AgreementInformation.sol
In order to store these information into a SharedObject you must use the property data:
sharedObject.data.information = "the information goes here"; |
The method flush() causes Flash Player to write the information in the respective data file. In case you don’t call it directly, the data will be persisted once you close the browser. Therefore, it’s a good practice to call it.
sharedObject.flush(); |
The clear() method clears the file and removes it from the local disk.
sharedObject.clear(); |
Check the complete example out:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationCompleteHandler(event)"> ? <fx:Script> [CDATA[ import mx.events.FlexEvent; ? private var sharedObject:SharedObject; ? protected function creationCompleteHandler(event:FlexEvent):void { sharedObject = SharedObject.getLocal("MyTextInformation"); if(sharedObject.size > 0) { information.text = sharedObject.data.information; currentDate.text = "Eu gravei essa data no objeto: " + sharedObject.data.currentDate; } } ? protected function saveClickHandler(event:MouseEvent):void { sharedObject.data.information = information.text; sharedObject.data.currentDate = new Date(); sharedObject.flush(); } ? ? protected function clearClickHandler(event:MouseEvent):void { sharedObject.clear(); } ? ]]> fx:Script> ? <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> s:layout> ? <s:Label id="currentDate" /> <s:TextArea id="information" /> <s:HGroup> <s:Button label="Salvar informação" click="saveClickHandler(event)" /> <s:Button label="Apagar informação" click="clearClickHandler(event)" /> s:HGroup> ? s:Application> |
References:
http://livedocs.adobe.com/flex/3/html/help.html?content=lsos_5.html
Hope you enjoy it!




