[ Team LiB ] |
Recipe 16.8 Checking for Updates to Remote Shared Objects16.8.1 ProblemYou want to be able to efficiently check to see what data has changed when a remote shared object is synchronized. 16.8.2 SolutionUse 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 DiscussionWhen 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:
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 AlsoSee Recipe 16.10 for important information on how to store data in the remote shared object. Also refer to Recipe 17.6. |
[ Team LiB ] |