Accessibility
Adobe
Sign in My orders My Adobe

Flex Documentation

Example 2


Table of Contents

  1. About local SharedObjects
  2. Creating a local SharedObject
  3. Example 1
  4. Example 2

The following example is similar to the first example, except that it shows that you can store simple objects, such as Array and Date, in the SharedObject without having to serialize and deserialize those objects:

The following example shows the contents of the lso2.mxml file:

<?xml version="1.0" encoding="iso-8859-1" ?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" initialize="initApp();">
  <mx:Script source="LSO2_script.as"/>
  <mx:VBox backgroundColor="white" borderStyle="solid" marginLeft="10" marginBottom="10" width="150">
  <mx:Label text="Color"/>
  <mx:TextInput id="myColor" width="100" />

  <mx:Label text="Scent"/>
  <mx:TextInput id="myScent" width="100" />

  <mx:Label text="Height"/>
  <mx:TextInput id="myHeight" width="30" />

  <mx:Label text="Last SetVal On"/>
  <mx:TextArea id="myLastDate" width="100" height="75" />
</mx:VBox>

<mx:Button label="Set Values" click="setVal();" />

</mx:Application>
The following example shows the contents of the LSO2_script.as file:
var myArray:Array;
var myLSO:SharedObject;

function initApp() {
  // Initialize Local Shared Object
  myLSO = SharedObject.getLocal("flowerValues");
  if (myLSO==null) {
    alert("Cannot create Local Shared Object","Error");
  } else {
    getVal();
  }
}

function getVal() {
  // Get value from LSO.
  myArray = myLSO.data.flowerArray;

  myColor.text = myArray[0];
  myScent.text = myArray[1];
  myHeight.text = myArray[2];

  myLastDate.text = myLSO.data.date;
}

function setVal() {
  // Set value in LSO.
  myArray[0] = myColor.text;
  myArray[1] = myScent.text;
  myArray[2] = myHeight.text;

  myLSO.data.flowerArray = myArray;
  myLSO.data.date = new Date();
  myLSO.flush();
}

For more complex examples of using the SharedObject object, see the Flex example applications in the samples.war file.