我有一个dojo.data.ItemFileWriteStore对象,其中的项目包含'children'属性,该属性又包含一个子项数组。我正在存储一棵树,我需要从这棵树中获取叶子节点。我已经编写了一个fetch方法,但它不会给它查询“children:[]”如何获取child.length为0的数据存储项(叶节点)?没有添加诸如'leaf'之类的属性的空白数组:bool到我的项目显然会有效,但我宁愿没有额外的属性。
dojo.require(dojo.data.ItemFileWriteStore);
// A tree node with no children, these are the kind I want returned
// from the query!
var rootItem = {
children: []
};
var treeStore = new dojo.data.ItemFileWriteStore({
data: {
items: [rootItem]
}
});
//when dojo reaches one of its inner filtering methods
//there is a point where it calls dojo.some() to see
//which elements in the array to return which match the
//given items attribute, this is where it fails
treeStore.fetch({
query: {children: []},
queryOptions: {deep: true},
onComplete: function(leafItems) {
// All the items with no children here...
}
});
我也尝试将属性的函数嵌套无效:
treeStore.fetch({
query: {
children: function(store, item){
return store.getValue(item, 'children').length == 0;
}
},
queryOptions: {deep: true},
onComplete: function(leafItems) {
// All the items with no children here...
}
});
答案 0 :(得分:1)
baseStore.fetch({
query:{id:'*'},
onComplete:function(a,b,c){
dojo.forEach(a,function(item,index){
console.log(item.children);
})
}
})
如果item.children未定义,则那些是您想要的“项目”。 如果您不想单独对此操作使用单独的提取,则在商店中使用名为_arrayOfAllItems的私有属性来获取平面数据存储,您可以在其上执行上述相同的条件。