Jun 6, 2009 6
Building a contextual recursive tree in Tapestry 5
UPDATE: There have been a few requests for a more complete source code example. The following link is to a demo application of the concepts from this entry, and the source code in this entry has been updated to reflect the content of the demo: Recursive Tapestry Tree Demo Application or fork from GitHub.
We use Tapestry 5 at work. Tapestry is a wonderful, efficient, and powerful framework, but like any piece of technology, there are headaches. The headache we had this past week was how on earth to render a tree of of components where we have to dynamically select the component depending upon the current context within the tree. Here’s the approach we uncovered using three components: a component acting as a container for the tree, another to render individual nodes of the tree (contextually subclassed for different nodes), and a custom delegate component (to turn off parameter caching).
The Container Component
The container component maintains stateful tree information while rendering the tree, and selects the component to render for the current node of the tree.
Here is the container’s template.
< ?xml version="1.0" encoding="UTF-8"?> <ul xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> <!-- The custom delegate to the component to render based on the context of the current node. --> <t :nodedelegate to="currentNodeRenderer"/> <t :block> <!-- This block contains a set of components to delegate rendering to based on the current node. --> <div t:id="blackNode"> <!-- Delegate to the child component to render. This replaces the component's body. --> <t :nodedelegate to="currentNodeRenderer"/> </div> <div t:id="redNode"> <t :nodeDelegate to="currentNodeRenderer"/> </div> </t> </ul>
Recent Comments