PDA

View Full Version : Dynamic Shared Objects


BigPoppa
07-12-2005, 03:21 PM
Can I set the value of an array to a Shared Object? Or, is there another way of creating shared objects dynamically?

ex:

myArray[i] = SharedObject.getRemote(soName, nc.uri, false);
myArray[i].onSync = function(list) {
trace("do something");
};
myArray[i].connect(nc);

Amn
07-13-2005, 02:06 PM
Sure you can.

Shared Object is just usual server side Actionscript object, and thus its reference can be assigned to anything you want. Only its inner workings are unknown, but the object itself is a native one.

BigPoppa
07-13-2005, 02:19 PM
see, that's what I thought, but when I try it, the onSync events don't seem to fire correctly, only the first one in the array does.

Amn
07-13-2005, 07:00 PM
Oh oh oh.

Its not that easy in your case. Let me explain:

1. First of all, it looks like we are dealing with client-side ActionScript code not server-side. Because SharedObject.getRemote is a method of a client-side SharedObject class.

2. In that case, be careful, because "getRemote" not always returns a valid reference, it may return "null" (See the reference for SharedObject.getRemote method)

3. Now, what happens when your array element is "null", and then you try to assign a property "onSync" to a "null" value ? Nothing.

4. So first, check that you actually got a valid reference to the object, and then you can safely assign properties to it, like the "onSync" even handler function (yes, it is a property too).

Like this:



var so = SharedObject.getRemote(soName, nc.uri); //third argument 'false' assumed by method by default

if(!so) throw new Error("Exception: 'so' is 'null'");

so.onSync = function() //This method will not be called until you initiate 'SharedObject.connect' method !
{
trace("so.onSync()");
};

myArray[i] = so;

so.connect(nc); //When the connection is established, 'so.onSync' will be called at least once, first, with 'clear' argument, indicating syncronisation of the client-side and server-side objects, and then maybe again, if additional synchronisation is pending (seldom case).



Try the above. It explains in detail what is going on. If you have further questions, fire them at me..

BigPoppa
07-13-2005, 08:29 PM
so, will I be able to refer to the data in each slot of the array as a shared object? and, will each slot be a unique shared object?


myArray[i].data["t_"+tutor_obj.username] = tutor_obj;

// or, more simply:

myArray[3].data[0] = "hello";

// or

trace(myArray[2].data[0]);

Amn
07-13-2005, 11:23 PM
Well, each SharedObject.getRemote call, provided you supply a unique object name as the first parameter, will return a (unique) reference to a unique sharedobject,.

So the following, given that SharedObject.getRemote always succeds:



myArray[0] = SharedObject.getRemote("so1");
myArray[1] = SharedObject.getRemote("so2");
myArray[2] = SharedObject.getRemote("so3");

//... for any 'i' and unique object name



Will store unique references to unique shared objects in slots of the array.

However, in practice i wouldn't recommend such a thing. Use SharedObject slots instead, not SharedObjects ;)

I didn't understand what your code sample was supposed to explain or tell me ? :)

the 'data' attribute is the actual data (object) of the SharedObject, and it can have properties. Each property is independent, and can also be an object with properties itself. However whenever a 'slot' (a property of 'data' object) changes, i.e. its value, or its properties if its an object itself, then the network synchronisation will bring overhead, because the WHOLE slot data is transmitted over network, not just the "property" that has changed. Be aware of that.