2015-12-28 107 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