Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive Variable Loading

Status
Not open for further replies.

Terwin

Programmer
May 23, 2002
51
0
0
US
I have a flash movie loading variables from a java servlet, and I have a question about how I can go about loading the data recursively.

The data is being loaded into a tree-like class, so I hope to be able to call the loading function recursively in order to populate the entire tree.

My difficulty is in making sure the variables from one load are loaded before the next iteration that declares a new loadVars object (with the same name). The code below seems to shoot through to the last possible Node before loading the data. Any ideas?

Thanks,
T

TreeClass.prototype.populate = function(currNode) {

nodeData = new LoadVars();
nodeData.onLoad = loadNodeData;
nodeData.goto = "getnodeinfo";
var i = 0;
while (i<currNode.childNodes.length) {
// Get nodeData relating to current child
if (nodeData.loaded != false) {
nodeData.nodeID = currNode.childNodes;
nodeData.sendAndLoad(appUrl, nodeData, GET);
}
}
function loadNodeData(success) {
if (success == true) {
// Instantiate Node Object
_root[&quot;Node&quot;+nodeData.nodeID] = new NodeClass(nodeData.nodeID, nodeData.childNodes, nodeData.whoId, nodeData.whoType, nodeData.actions, nodeData.methodIn, nodeData.documents);
_root[&quot;Node&quot;+nodeData.nodeID].dumpNode();
i++;
populate(_root[&quot;Node&quot;+nodeData.nodeID]);
}
}
 
When using sendAndLoad you have to have separate send and load loadVars objects - one to format and broadcast the data and one to receive the requested info from the server, at the moment you don't have an object to receive the new data, you're just referencing the object which is doing the sending.
 

Flash actually seems to be able to load variables into the same LoadVars object that sent them.. but your point is well taken from an organizational perspective.

The chief difficulty I'm having, however, is controlling the loading of the nodeData so that the objects have time to populate (and properly instantiate themselves from the populated data) before the next nodeData object is declared and the sendAndLoad request is blown away.

How can I use the calling function (populate, in this case) and the onLoad handlers to bounce control back and forth without destroying sendAndLoad requests that haven't yet completed? Basically, populate is called on the root node, which needs to execute a sendAndLoad on each of its children, recursively calling populate on those children once they are instantiated - but the routine shoudln't move on to the recursive call (or the loading of the next child) until the previous load is completed.

I'm still struggling with this one, so any help is greatly appreciated.

T
 
My first thought is to write an XML document from the data supplied by the servlet and deliver that to your Flash movie - which would maintain the tree structure and it's easy to write a recursive routine to parse the XML.

Other than that maybe create a class in Flash which instantiates multiple loadVars objects dynamically from within a loop? Then each new instance can have a unique identifier and delete itself once it has loaded and dealt with its own section of the incoming data. If each object is handling a discrete parcel there should be no problems of things getting lost or overwritten.

This could get problematic depending on how much data is coming in as the process of object creation and destruction is one of the heaviest loads on the Flash player.

Haven't tried this out but supposedly something like:

for(var i=1;i<10;i++){
this['dataObject'+i]=new LoadVars();
}

..shouldn't work.
 
I was going to try and avoid using XML because I've had so much irritation with the Flash XML Object.. mostly its useless ignoreWhite method and having to constantly check for null nodes. I was actually just playing around with instantiating many unique LoadVars objects in the style you suggested. While it seems like it will work, it's definitely clumsy. Looks like XML is the way to go for dealing with this sort of situation.

Thanks for your help.

T
 
In case anyone's interested, I succeeded with wangbar's original suggestion of dynamically generating LoadVars objects with unique and predictable names. I actually replaced the custom objects representing tree nodes with LoadVars objects (which do the job just fine since it was essentially a struct anyway). Code follows, thanks to both gents who answered me. billpower - that solution looks like a good one, but is probably best reserved for problems requiring extremely large data loads (not the case here). Code follows.

T

TaskTreeClass.prototype.populate = function(currNode) {
// Starting with the rootNode, recurse through the whole tree, populating nodeData objects along
// the way. nodeData objects are named as nodeData + i + j where i is the ID of the parent node,
// and j is the position of the current node in the parent's childNodes array.
currNode.childNodes = currNode.childNodes.split(&quot;,&quot;);
if (currNode.childNodes.length != 0
&& currNode.childNodes[0] != &quot;null&quot;) {
for (i=0; i<currNode.childNodes.length; i++) {
_root[&quot;nodeData&quot; + currNode.id + i] = new LoadVars(); // LoadVars Object to Send
_root[&quot;nodeData&quot; + currNode.id + i].goto = &quot;getnodeinfo&quot;;
_root[&quot;nodeData&quot; + currNode.id + i].nodeID = currNode.childNodes;

_root[&quot;nodeDataLoad&quot; + currNode.id + i] = new LoadVars(); // LoadVars Object to Load
_root[&quot;nodeDataLoad&quot; + currNode.id + i].onLoad = loadNode; // LoadVars Event Handler
_root[&quot;nodeData&quot; + currNode.id + i].sendAndLoad(appUrl, _root[&quot;nodeDataLoad&quot; + currNode.id + i], GET);
}
}

function loadNode(success) {
if (success) {
debugOut(&quot;Loaded Node Successfully -- ID = &quot; + this.id + &quot;\n&quot;);
if (this.childNodes != null) {
_root.mainTree.populate(this);
}
} else {
debugOut(&quot;Failed to Load nodeData\n&quot;);
}
}

};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top