grailslazy-loadingdijit.tree

Didn't get dijit.Tree to lazy loading


I need to lazy-load the directories into a dijit.Tree which is in a dijit.form.Dialog. I get the root directories and I can put them into the tree with the root node 'Computer', but I can't figure out how to get the lazy-loading to work.

When I click on a node, I call a function which makes an ajax call to get the children of the selected node. I can fetch them and render them as JSON back to the gsp (I think they are well formed). But when I try to put them into the store, it fails. I can't get store.newItem() to work. I don't really know if it's the right and easiest way to do this anyway. I searched and tried and failed and unfortunately, I'm stuck.

I use:

The way how I get the dataStore to the gsp:

    File [] roots = File.listRoots()        
    def item = []

    roots.each{
        item << [
            name         : it.getAbsolutePath(),
            path         : it.getAbsolutePath(),
            children     : []
        ]
    }
    def store = [
        identifier: 'path',
        label: 'name',
        items: item
    ]

    def retVal = [store: store]

    render retVal as JSON

Definition of store, model and tree:

    function prepare(dataStore) {                           
        var store = new dojo.data.ItemFileWriteStore({
               data: dataStore
        });
        var treeModel = new dijit.tree.ForestStoreModel({
               store: store,
               rootId : "root",
               rootLabel : "Computer",
               childrenAttrs: ["children"]
       });
       var treeControl = new dijit.Tree({
               id: 'directoryTree',
               model: treeModel,
               autoExpand: false,
               onClick: loadDirectories
               },
               "treeOne");
       }

Here the code how I try to edit the store, data is a list:

    data = [
        [name: 'info', path: 'C:\temp\info', children: []]
        [name: 'grmpf', path: 'C:\temp\grmpf', children: []]
        ...
    ]

    for(i=0; i < data.length; i++){
      store.newItem({name: data[i].name, path: data[i].path, children: data[i].children}, {parent: parent.path[0], attribute: 'children'}); 
      store.save(); 
    }   

I tried it this way and once, I deleted the item, edited the item and put it into the store again. But nothing worked..

If anyone can give me any insights, I would appreciate it!


Solution

  • Fortunately, I found the very strange problem...

    Well, I tried :

        store.newItem(jsObj, parent);
        store.save();
    

    The solution:

        model.newItem(jsObj, parent);
    

    Instead of adding the item to store, I put it into the model and then it works. I really do not know why. Maybe someone can explain it to me. Thanks