Date: September 25, 2006
Component: TreeView, Event
Version: v0.11.3
This simple example of a TreeView using dyanmic loading responds to jmpow99's question on the YDN-JavaScript group (and followup question here).
The followup question asks about how to determine what node is involved when a label is clicked on and how to track information about that node. Let's start with the issue of associating data with a Node object. The constructor for a TextNode takes as its first argument the Node label (as a string) or, alternatively, a custom object containing data associated with the Node; when a custom object is passed, the Node instance will use the object's label member as the label and, if present, the href member as the "destination" for a label click. But we can put in additional members, too — anything that we want to associate with the Node. So, in the above implementation, we'll specify our label and we'll note that in this object literal we could put any other custom attributes we'd care to:
Now, how to get to that data when a label is clicked? That custom object we created will be stored as the Node instance's data member. And YAHOO.widget.TextNode has a method called onLabelClick which fires each time a label is clicked and which receives the label's parent Node as an argument. We can write our own function to handle this and assign it to our Node instances' onLabelClick member:
Note that in our onLabelClick function we're extracting just the label member from our Node's data object, but any additional data that we stored there would be accessible to us at that point — including, for example, the unique identifier that might tie that node to its corresponding database record.
In total, our code now looks like this:
— Eric Miraglia, Yahoo Presentation Platform Engineering
(c)2006-2007 Yahoo Inc.