PDA

View Full Version : About creating complex objects dinamically



Hory
4 Aug 2007, 4:06 PM
I have a list of trees. I'm trying to make it so that each of them is openable by the user in a separate tab (panel) in the center region. These trees should also support various functions (delete nodes, create new nodes, etc.) which are started by a separate toolbar. In order to access one of these functions of a particular tree, I assume I have to put each tree in distinct objects.

Is this the best method or is there a way to control particular trees without defining the same methods for each of them? And if I generate a different object for each tree which is opened, the name of the object must vary. How do I name that object dinamically in a way that I can later reference? (eg. Tree1, Tree2)?

Thanks for reading, I hope you understand what I mean.

BernardChhun
4 Aug 2007, 4:36 PM
first of all you don't have to redefine any events handler for your trees.

the code below is explicit enough I hope.


var treeOnClickHandler = function(cn){
// "cn" is the clicked node
alert(["look at me! ",cn.id].join(" : "));
cn.expand();
}

tree1.on("click", treeOnClickHander, this, true);
tree2.on("click", treeOnClickHander, this, true);

in short, you have to make your event handlers as general as possible so that it may be re-used later on. If you plan on doing some specific things depending on the tree, I'd recommend to fork things out depending on your tree's id. I often use different prefixes for my ids such has : tree1_node-id. I then split the id and act accordingly.

as for keeping all your trees in memory, I wouldn't do it if you have lots of nodes.