we would like to create a tree in MVVM pattern.
And this tree should have 2 directories passed by parameters.
The goal is to create a directory explorer, and we would like to have a “load on demand” when a child is opened in order to have better perfomance.
For moment, we find the exemple in documentation but it is incomplete :
public class TreeSelectionVM {
private TreeModel<TreeNode<String>> itemTree;
private String pickedItem;
//omit getter and setter for brevity
}
<window apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('org.zkoss.reference.developer.mvvm.collection.TreeSelectionVM')">
<tree id="tree" model="@bind(vm.itemTree) " width="600px"
selectedItem="@bind(vm.pickedItem)">
<treecols>
<treecol label="name" />
<treecol label="index" />
</treecols>
<template name="model" var="node" status="s">
<treeitem open="@bind(node.open)">
<treerow>
<treecell label="@bind(node.data)" />
<treecell label="@bind(s.index)" />
</treerow>
</treeitem>
</template>
</tree>
</window>
Should we manage the onOpen event ? or should we implements TreeModel and TreeNode methods (getChild & co) ?
Thank you.
Here is a more complete zk mvvm tree example which is along the lines of a directory explorer which does a load on-demand:
https://github.com/simbo1905/zktreemvvm
Just check it out with “git clone” then run it with:
The code will start its own jetty server on a given port.
It uses Apache Commons VFS as an abstraction to a filesystem rather than using raw java.io.File which is harder to mock and unit test. By default the code displays a ZK screen to browse the inside of commons-vfs2-2.0.jar as though it were regular files and folders. Simply change the FILE_SYSTEM_URI uri in the ViewModel class to be file location like “file:///some/path/” to view a regular filesystem.
The zul page contains a tree and binds it’s model to vm.treeModel and it’s selectedItem event to the vm.pickedItem method:
The @Converter does the friendly rendering of the FileObject data held in the tree model.
The actual ViewModel does not do much but hold the selectedItem event handler (which does nothing but log) and provides access to the TreeModel:
The main work is in the VfsTreeModel class which extends AbstractTreeModel and defines the mandatory methods
There was a recommendation in this bug tracker to override the getPath to prevent the framework making a lot of other calls to figure out that infomation:
http://tracker.zkoss.org/browse/ZK-1278
That method is implemented as a walk to the root:
In the following Tree Model documentation and the “Huge Data” documentation it recommends caching:
http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVC/Model/Tree_Model
http://books.zkoss.org/wiki/ZK%20Developer%27s%20Reference/Performance%20Tips/Listbox,%20Grid%20and%20Tree%20for%20Huge%20Data/Implement%20ListModel%20and%20TreeModel
So in the example code the ViewModel creates a caching subclass of the VfsViewModel. Refreshing the page would create fresh objects and let the cache be garbage collected which is probably good enough for this example.