2015-12-28 105 views
0

我有一个dijit的树代表菜单项, 一些有孩子的,其他的都是叶子节点。道场dijit树:如何管理父母,孩子和叶节点?

我想知道如何写这个在JavaScript中,哪个属性,当我有孩子的正常节点和叶节点使用? 如何写:在这种情况下使用的文件夹图标,并在其他叶图标。

有我的代码:

<script> 

require([ 
    "dojo/_base/window", "dojo/store/Memory", 
    "dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo", 
    "dojo/domReady!", "dojo/parser" 
], function(win, Memory, ObjectStoreModel, Tree, dojo){ 

    // Create test store, adding the getChildren() method required by ObjectStoreModel 
    var myStore = new Memory({ 
     data: [ 
      { id: 1, name: 'Menu', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', root: true, directory: true }, 
      { id: 2, name: 'Folder1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', parent: 1, directory: false}, 
      { id: 3, name: 'Leaf1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree.model', parent: 2 }, 
      { id: 4, name: 'Leaf2', url: 'http://dojotoolkit.org/api/1.6/dijit.tree.ForestStoreModel', parent: 2 }, 
      ... 
      ], 
     getChildren: function(object){ 
      return this.query({parent: object.id}); 
     } 
    }); 

    // Create the model 
    var myModel = new ObjectStoreModel({ 
     store: myStore, 
     query: {root: true} 
    }); 

    // Create the Tree, specifying an onClick method 
    (new Tree({ 
     model: myModel, 
getIconClass:function(item, opened){ 
       return myStore.getValue(item, 'directory') ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "dijitLeaf"; 
      }, 
     onClick: function(item){ 
      // Get the URL from the item, and navigate to it 
      // location.href = item.url; 
      //parent.getElementById('central').src = item.url; 
      parent.central.location.href = item.url; 
     } 
    })).placeAt(win.body()).startup(); 
}); 
</script> 

但它不工作。
我试图与第一和内部的MyStore第二项的目录属性,但加载getIconClass时,我得到一个错误:“的MyStore是不确定的”。

感谢您的帮助

回答

0

看起来你是混合遗产道场/数据/存储的使用和新的道场/存储的API。 dojo/store apis上没有getValue方法。

这里是一个工作版本:

// Code goes here 

要求([ “道场/ _base /窗口”, “道场/ DOM”, “道场/存储/内存”, “的dijit /树/ ObjectStoreModel”, “的dijit /树”, “道场/ domready中!” ]功能(WIN,DOM,内存,ObjectStoreModel,树,道场){

// Create test store, adding the getChildren() method required by ObjectStoreModel 
var myStore = new Memory({ 
    data: [ 
     { id: 1, name: 'Menu', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', root: true, directory: true }, 
     { id: 2, name: 'Folder1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', parent: 1, directory: false}, 
     { id: 3, name: 'Leaf1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree.model', parent: 2 }, 
     { id: 4, name: 'Leaf2', url: 'http://dojotoolkit.org/api/1.6/dijit.tree.ForestStoreModel', parent: 2 } 
     ], 
    getChildren: function(object){ 
     return this.query({parent: object.id}); 
    } 
}); 

// Create the model 
var myModel = new ObjectStoreModel({ 
    store: myStore, 
    query: {root: true} 
}); 

// Create the Tree, specifying an onClick method 
(new Tree({ 
    model: myModel, 
    getIconClass:function(item, opened){ 
      // You already have the item. No use to try and refetch it from the store 
      return item.directory ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "dijitLeaf"; 
     }, 
     onClick: function(item){ 
      // Get the URL from the item, and navigate to it 
      parent.central.location.href = item.url; 
     } 
    })).placeAt(win.body()).startup(); 
}); 

http://plnkr.co/edit/11Z20MpsZyShh1E0Ai7k?p=catalogue