Recipe 16.8 Checking for Updates to Remote Shared Objects

16.8.1 Problem

You want to be able to efficiently check to see what data has changed when a remote shared object is synchronized.

16.8.2 Solution

Use the list parameter that is automatically passed to the onSync( ) method. Loop through the parameter to locate all the elements in which the code property has the value indicating the type of update you are interested in.

16.8.3 Discussion

When the onSync( ) method is invoked (see Recipe 16.6), it is automatically passed a parameter that contains information about how the data has changed. The parameter is an array of objects, one for each property of the remote shared object. For example, if a remote shared object has two properties named myDate and myArray (meaning my_r_so.data.myDate and my_r_so.data.myArray), then the list parameter passed to the onSync( ) method will have two elements. In turn, each element contains three properties: code, name, and oldVal. One element will have its name property set to "myDate", and the other will have its name property set to "myArray". The code property can have the following values for persistent shared objects:

"success"

The server-side value was successfully updated from a change made by this client.

"reject"

The client tried to make a change to the value, but it was rejected by the server usually because the same object was being updated by another client.

"change"

The client-side value has been updated based on a new value from the server.

"delete"

The property has been deleted.

"clear"

Either the object is not persistent on the server or on the client, or all the properties have been deleted.

This change information is useful when you want to make changes in the client movie following a synchronization event, and you need to determine what data changed and how. For example, in a whiteboard application, you might use an RSO to store the positions of various movie clips that can be controlled by multiple users. Each movie clip's data can be stored as a different element in the RSO. When one client updates the position of a movie clip, you want to update that movie clip's position in all other connected clients. However, you don't want to unnecessarily update the positions of other movie clips that have not been moved. The following client-side code defines an onSync( ) method that determines which movie clips moved and updates their positions accordingly:

myClient_r_so = SharedObject.getRemote("myFirstRSO", myConnection.uri, true); myClient_r_so.connect(myConnection); myClient_r_so.onSync = function (list) {   for (var i = 0; i < list.length; i++) {     // Synchronize changes to a movie clip's position across all connected clients.     // Check only for elements marked as "change" because the movie clip's properties     // will be changed locally already for those marked "success".     if (list[i].code == "change") {       // This code assumes that the name of the elements in the shared object       // correspond to the names of movie clips in _root.       _root[list[i].name]._x = this.data[list[i].name]._x;       _root[list[i].name]._y = this.data[list[i].name]._y;     }   } };

Of course, the preceding code does not work in isolation. It requires that the client also contain code that writes to the RSO each time a client moves a movie clip. For example:

myMovieClip_mc.onPress = function (  ) {   this.startDrag(  ); }; myMovieClip_mc.onRelease = function (  ) {   this.stopDrag(  );   // Create a new property in the RSO with the same name as the movie clip. Set that   // property's value equal to an object with _x and _y properties.   _root.myClient_r_so.data["myMovieClip_mc"] = {_x: this._x, _y: this._y};    };

16.8.4 See Also

See Recipe 16.10 for important information on how to store data in the remote shared object. Also refer to Recipe 17.6.



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net