Data Management Using Slots

The data attributes of a SharedObject (or slots) can contain any basic ActionScript or JavaScript variable type. Two of the most common types include arrays and objects because of their capability to store organized complex data. You will get the most benefit from Flash MX and the Flash Communication Server if you understand basic object-oriented programming used in ActionScript and JavaScript. The full scope of the object model is very powerful, but understanding the basic model for data storage is simple. Figure 8.6 is a simplified overview of how you can use slots within the data attribute.

Figure 8.6. How the data attribute can store a collection of ActionScript variable types.

graphics/08fig06.gif

In Flash circles, the term slot could be a property of a MovieClip. Or in the case of the SharedObject, a slot refers to a variable slot off the data attribute. These slots are what the Flash Communication Server monitors for change. In the previous exercise, simple variable slots are used to store the shared properties of the MovieClip. We could have placed these properties within an object; however, Flash Communication Server would report that the entire object had changed each time a property value was modified. It was much more efficient to use a different slot for each MovieClip property. Using this method, significant bandwidth was saved.

Consider a more complex example. Let's say you contain an object with 10 properties within an array position. This is very similar to structure of the information object handled by the onSync method. Each time a property within any embedded object is changed, the server announces a change to the entire stub (which in this case is the entire array). Each Flash player then is responsible for downloading the entire array and parsing it to reflect the change.

You are seeing now the need for some data management. That is a strategy you are responsible for planning. Chapter 15, "Communication Application Architecture Strategies," will discuss some strategies for handling larger amounts of data using the SharedObject architecture.

Objects?Properties?Values: Understand This Model

The relationship between objects, properties, and values is important to know, especially when bandwidth becomes an issue. Read the case below to help you understand the lexicon and the concept of objects in programming.

Consider a cardboard box as an object. This box has properties that include height, width, weight, volume, and so on. Each property has a value. Height = 5 inches, width = 10 inches, weight = 5 lbs, and volume = 500 cubic inches. Some properties, such as height and width, cannot be changed without a pair of scissors and some duct tape. When you put something into the box, this changes the value of its weight property.

Now, put this into the context of synchronization and the Flash Communication Server. The only property that can change with this box object is the weight. When someone puts something in it, the weight changes. If this box exists in the Flash environment, the Flash Communication Server would announce to every Flash player that the box object has changed, with no mention to the property weight. Flash players respond by accessing the box object and retrieving its properties.

How would this look as ActionScript code? There are two ways to set up the box object. These few lines of ActionScript create a new myBox object with four properties, each with initial values:

myBox = new Object(); 
myBox.height=5;
myBox.width=10;
myBox.weight=10;
myBox.volume=500;

A simplified technique can also be used to set up the initial properties and values of an object using a single line of code. The following example does exactly the same as the previous example:

myBox = new Object({height:5,width:10,weight:10,volume:500}); 

To make it even simpler, ActionScript automatically declares the variable as a new object when you pass this structure:

myBox = {height:5,width:10,weight:10,volume:500}; 

Advanced users could develop a myBox prototype containing all the constant values with a method to retrieve the weight property, and then "instance" the box object to myBox. But we won't do that here, just to keep it simple. Transferring the object to a remote SharedObject is easy. Simply copy the entire object to the data property. This example creates a new, non-persistent, remote SharedObject on the NetConnection called myRemoteObject, and then sends the myBox object as a slot of the data property.

// setup the shared object using the "my_Connection" NetConnection. 
   my_so = SharedObject.getRemote("myRemoteObject", myConnection_nc.uri, false);
   my_so.connect(myConnection_nc)

// attach the myBox object to the SharedObject
my_so.data = myBox;

You can see that interacting with the SharedObject is just like interacting with client-side variables.

Accessing the remote data is easy using some simple programming techniques. This line of code returns the value of the weight property stored in the myBox object at the server:

var myLocalVar = my_so.data[myBox].weight; 

This line of code writes a new value to the weight property on the server:

my_so.data[myBox].weight = myLocalVar; 

Deploying some smart SharedObject management increases performance of your application, and decreases the load on bandwidth and server resources. Remember that each time you read/write data to the remote SharedObject, you are communicating over the NetConnection. You should practice conservation by only storing objects and properties that will be known to change. Achieving this technique involves applying some home-grown strategy to the synchronization process. This book will present a few strategies along the way, but you can apply any strategy you wish?that is the largest benefit of object-oriented programming.

Suggestions?What You Can Do with the Remote SharedObject

Remote SharedObjects require the most creative thinking to put them to use. Because they are so flexible, you have a lot of opportunities. Here are some ideas on how you can use the remote SharedObject in your applications:

  • Network games

  • Presentations

  • Internal communication

  • Collaborative workflow

  • Security cameras



    Part I: 10 Quick Steps for Getting Started
     
    ASPTreeView.com
     
    Evaluation has єПФЧexpired.
    Info...