View comments | RSS feed

SharedObject.data

Availability

Usage

myLocalOrRemote_so.data

Description

Property; the collection of attributes assigned to the data property of the object; these attributes can be shared and/or stored. Each attribute can be an object of any of the basic ActionScript or JavaScript types--Array, Number, Boolean, and so on. For example, the following lines assign values to various aspects of a shared object:

var items_array:Array = new Array(101, 346, 483);
var currentUserIsAdmin:Boolean = true;
var currentUserName:String = "Ramona";

var my_so:SharedObject = SharedObject.getLocal("superfoo");
my_so.data.itemNumbers = items_array;
my_so.data.adminPrivileges = currentUserIsAdmin;
my_so.data.userName = currentUserName;

for (var prop in my_so.data) {
    trace(prop+": "+my_so.data[prop]);
}

All attributes of a shared object's data property are available to all clients connected to the shared object, and all of them are saved if the object is persistent. Attributes are volatile; if one client changes the value of an attribute, all clients now see the new value. The shared object created above contains the following information:

userName: Ramona
adminPrivileges: true
itemNumbers: 101,346,483

NOTE

 

Do not assign values directly to the data property of a shared object, as in my_so.data = someValue; Flash ignores these assignments.

To delete attributes for local shared objects, use code such as delete so.data.attributeName; setting an attribute to null or undefined for a local shared object does not delete the attribute.

To create private values for a shared object--values that are available only to the client instance while the object is in use and are not stored with the object when it is closed--create properties that are not named data to store them, as shown in the following example:

var my_so:SharedObject = SharedObject.getLocal("superfoo");
my_so.favoriteColor = "blue";
my_so.favoriteNightClub = "The Bluenote Tavern";
my_so.favoriteSong = "My World is Blue";

for (var prop in my_so) {
    trace(prop+": "+my_so[prop]);
}

The shared object contains the following data:

favoriteSong: My World is Blue
favoriteNightClub: The Bluenote Tavern
favoriteColor: blue
data: [object Object]

Example

The following example saves text from a TextInput component instance to a shared object named my_so (for the complete example, see SharedObject.getLocal()):

// Create a listener object and function for the <enter> event.
var textListener:Object = new Object();
textListener.enter = function(eventObj:Object) {
    my_so.data.myTextSaved = eventObj.target.text;
    my_so.flush();
};

Comments


MasahiGo said on Jun 21, 2006 at 11:58 PM :
I bet I'm not the only one wondering if it's possible to declare the attribute
field - names during runtime dynamically instead of having to name the attributes in advance. Well, since I couldn't find any example whatsoever of this, I thought I'd make one myself. My deepest gratitude goes to the webmaster of flashvalley.co.uk, who helped me in solving this problem.

We want to add attributes to a shared objects data property dynamically. And of course we want to name the attributes after something that refers to the contents we put in them.

Let's modify the livedocs example a bit:

First, we can define the variables (String) that we use as the attribute names (could be assigned/defined later and/or read from XML-file etc.)

var itemNum:String = "itemNumbers";
var adminPriv:String = "adminPrivileges";
var userN:String = "userName ";

Then we define the variables that will be assigned to attributes.

var items_array:Array = new Array(101, 346, 483);
var currentUserIsAdmin:Boolean = false;
var currentUserName:String = "Masi";

var my_so:SharedObject = SharedObject.getLocal("superfoo");

And finally, we use the variables (type String) to name the attribute fields:

my_so.data[itemNum] = items_array;
my_so.data[adminPriv] = currentUserIsAdmin;
my_so.data[userN]= currentUserName;

for (var prop in my_so.data) {
trace(prop+": "+my_so.data[prop]);
}

And this will print:

userName: Masi
adminPrivileges: false
itemNumbers: 101,346,483

I hope this will help others like me.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/fms/2/docs/00000604.html