16.2. Adding Data to a Client-Side Shared Object

Problem

You want to add data to a client-side shared object (whether it be a remote or local shared object).

Solution

Add the values as properties of the shared object’s data object.

Discussion

Shared objects have a special built-in property named data. The data property is an object to which you should add any information that you want to save to the shared object:

// Store a username value to the shared object.
mySO.data.username = "Joey";

Properties attached directly to the shared object, rather than to its data property, are not written to the shared object. Thus, attaching properties directly to the shared object is useful only for data that does not need to persist between sessions. Therefore, the following code is potentially an erroneous attempt to save data:

mySO.someVal = "this value is lost when the movie closes";

The correct approach is to attach the value to the data property, as follows:

mySO.data.someVal = "this value is stored in the shared object";

You can store several native ActionScript datatypes to the shared object’s data property, as follows.

mySO.data.myArray = new Array("a", "b", "c");
mySO.data.myDate = new Date(  );

However, you cannot store movie clips, buttons, text fields, or shared objects themselves to a shared object’s data property such that the data persists correctly between sessions.

You can also store objects created from custom classes to a shared object’s data property. However, for the object to be properly ...

Get Actionscript Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.