[Zope3-dev] Re: howto zope.app.tree

Philipp von Weitershausen philipp at weitershausen.de
Tue Jul 24 13:48:04 EDT 2007


(Peter, this is the wrong list for questions concerning how to *use* 
Zope. Please write to zope3-users in the future.)


Peter Füreder wrote:
> We are trying to implement a dynamic tree with zope.app.tree.
> We want to fetch a list of objects from a database and display them [as 
> a link for example].
> When this object is selected, we want to fetch the children an display them.
> 
> We haven't been able to find any example save the browser/cookie.py of 
> the zope menu which seems to use a static tree implementation.
> It also relies on the fact that the displayed objects are persistent in 
> the ZODB.

The example implementation is indeed geared towards the ZODB, 
zope.app.tree is completely storage agnostic, though. In fact, it was 
originally developed for an SQL-based storage.

> If we had to prefetch all the potentially displayed objects for the 5 
> levels we wish to traverse, it could lead to truely many objects.

You don't have to. zope.app.tree will only fetch the sub-objects of 
*expanded* nodes. If nodes aren't expanded, it won't even look at them.

> We've tried to implement adaptors for the IChildObjects interface but we 
> are not sure how to tie them into the system.

Using ZCML, like any other component:

   <adapter factory="path.to.adapter.TheAdapter" />

> But maybe that's the wrong approach anyway.

It is the right approach.

Basically, you'll have to do two things:

* Implement the IUniqueId adapter for any object that may end up in the
   tree.

* Implement the IChildObjects adapter for any object that may have
   sub-nodes.

* Pick some root object and create the root node:

     root_node = Node(root_obj, expanded_nodes, filter)

   where expanded_nodes is a list of unique ids of nodes that should be
   expanded by default and filter is an optional filter object.

   You probably also want to expand the root node by default:

     root_node.expand()

   Then you can construct the tree in a ZPT by iterating over the root's
   child nodes. See the INode interface for the whole API.


-- 
http://worldcookery.com -- Professional Zope documentation and training


More information about the Zope3-dev mailing list