0

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:

  • Grails 1.3.9
  • Dojo Toolkit 1.6

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!

Melanie
  • 21
  • 2
  • 9
  • Please show some critical code. – AA. Sep 05 '12 at 16:01
  • I edited my entry and put some code in it. If you need more, please ask – Melanie Sep 06 '12 at 06:09
  • any errors in firebug console? – maialithar Sep 06 '12 at 06:24
  • @h4b0 : Yeah, "dojo.data.ItemFileReadStore: Invalid item argument", but I don't know what of the item is invalid – Melanie Sep 06 '12 at 06:50
  • add some `console.log` outputs before `store.newItem()`: `console.log(data)`, `console.log(data[i])`, `console.log(data[i].name)`, `console.log(data[i].path)` and `console.log(data[i].children`. what is `parent.path[0]`? – maialithar Sep 06 '12 at 06:56
  • @h4b0: I have some console.log outputs there and everything seems fine. Every data had a name, a unique path and 'nothing' for children. Children should be an empty list. parent is the node where I need to put the data into. And parent.path[0] is the unique path, so that the store knows where to put the data – Melanie Sep 06 '12 at 07:04
  • problem seems to be with your `ItemFileReadStore`. add your store definition to your question. – maialithar Sep 06 '12 at 07:24
  • do you use `dojo.data.ItemFileReadStore` anywhere in your code? please post `loadDirectores()` (if it's not too long) and your json data. could you set it all up on jsfiddle? – maialithar Sep 06 '12 at 09:40
  • @h4b0 No, I don't use it. I used it, but then I got the Error that 'store.newItem()' is not a function. So I changed it to ItemFileWriteStore. I will need some time to set it up on jsfiddle. I never used it.. – Melanie Sep 06 '12 at 09:46
  • @h4b0 I don't know if I used jsfiddle the right way... :-/ http://jsfiddle.net/mWuPS/ – Melanie Sep 06 '12 at 10:20
  • I fixed your jsfiddle, but I'm unable to solve your problem : / http://jsfiddle.net/mWuPS/3/ – maialithar Sep 06 '12 at 11:49

1 Answers1

0

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

Melanie
  • 21
  • 2
  • 9