- The Book of Dojo
- The Dojo Book, 0.4
- Part 1: "Introduction"
- Part 2: "Out of the Box" Dojo
- Part 3: "The Dojo Programming Model"
- Part 4: "More on Widgets"
- Part 5: "Connecting the pieces"
- Part 6: "Customizing Dojo Builds for Better Performance"
- Part 7: "Utilities"
- Part 8: "Internationalization and Accessiblity"
- Part 9: "Dojo Community"
- Part 10: "Fresh From The Shed" Dojo
- BookWriting
- Glossary
2 Connecting an Action to a Node
Submitted by criecke on Sun, 03/25/2007 - 12:05.
The problem is our tree does nothing but stand around looking beautiful. Nothing wrong with that. Normally, though, you'd want some kind of action to occur when the node is clicked. To do this, you can use the TreeSelector Widget.
TreeSelector is a widget without a UI. You use it as a placeholder for connecting the tree to various Javascript actions. This makes it easy to construct many trees, and connect them to the same actions.
<script>
dojo.addOnLoad(function() {
dojo.event.topic.subscribe("nodeSelected",
function(message) { alert(message.node.title+" selected"); }
);
});
</script>
<div dojoType="TreeSelector" widgetId="tSelector" eventNames="select:nodeSelected" ></div>
<div dojoType="Tree" selector="tSelector" >
<div dojoType="TreeNode" title="Item 1">
<div dojoType="TreeNode" title="Item 1.1" ></div>
<div dojoType="TreeNode" title="Item 2">
</div>
(Is there an easier way to do this??? -- CAR)
When you click on a node, an alert box will pop up with the name you selected.
- Printer-friendly version
- Login or register to post comments
- Unsubscribe post

Tree failing to display?
I found I had to alter the Tree Selector div to have this at the end:
...select:nodeSelected"> </div>Rather than:
...select:nodeSelected"/>Otherwise FireFox 2 was not rendering my tree. After I did this modification it worked fine.
Thank you!
Fixed now. And this is a good reminder for us to test all the examples in FF 2.0 for Dojo Dot Book 0.9.
More than one eventname binding?
Hi,
I just stumbled across a weird problem:
As soon as I try binding more than one event name in the eventNames argument, the events won't fire at all. Example:
works, but:
does not!
In the latter case, neither of the two events fire. How come? Ain't this just a plain JavaScript object I am passing to this argument?
Problem Solved
Eh, okay maybe I should use the debugger before asking questions. The solution is to pass eventName bindings this way:
eventNames="eventA:myEventA;eventB:myEventB"
So, you have to use semicolons instead of commas to separate the bindings. Not very intuitive since this argument is parsed as an associative array (i.o.w. as an object) somewhere internally, so you should really be able to use JSON here.
This should be documented somewhere.