This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
288
|
Chapter 8: Shared Objects
remote_so.data.line_20[4] = 4;
var userName = "peldi";
var user1 = {fullName: "Giacomo Guilizzoni", status: "Online"};
var user2 = {fullName: "Giacomo Guilizzoni", status: "Online"};
// When adding a new property, the slot will be created on all copies.
remote_so.data[userName] = user1;
// Assigning a different object with identical contents will force an update.
remote_so.data[userName] = user2;
When an object or array in a slot is updated, a new copy of that object or array is cre-
ated in all the remote shared objects (except in the movie where the slot was
updated). The old object or array is replaced with a new one. This can produce sur-
prising results if you are using a reference to an object or array in a shared object slot.
For example, if one movie creates a reference to an object in a shared object slot this
way:
// Movie #1 gets a reference to an object in a slot.
var peldi = this.data.peldi;
then another movies updates the same slot in its copy this way:
// Movie #2 changes the status property of the object in the same slot.
this.data.peldi.status = "Offline";
When the update is complete, the peldi object will not refer to the same object as the
one stored in
remote_so.data["peldi"]. After the update, the following test will pro-
duce the results listed after each statement:
// Movie #1 now has two separate objects with a different peldi property.
trace(peldi == this.data.peldi); // false
trace(peldi.status); // Online
trace(this.data.peldi.status); // Offline
To fix this problem, the old reference should be overwritten with a reference to the
new object once the change has occurred (see the next section for how to detect a
slot change):
// Movie #1 retrieves the most recent object in the slot.
peldi = this.data.peldi;
The ActionScript garbage collection process will dispose of the old object.
onSync( ) and Managing Change
Whenever the content of a shared object slot is created, updated, or deleted in one
movie, the changes are sent to the server. If the update is successful, every movie
connected to the same shared object will eventually have its copy of the slot updated.
Updates occur at set intervals (and only if changes were made), so several slots may
be updated in one batch. When the updates for one interval are complete, the
onSync( ) method of the shared object is called and passed an array of information

Get Programming Flash Communication Server now with O’Reilly online learning.

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