gpt4 book ai didi

javascript - 如何禁用 Dojo 树中每个节点扩展上的缓存/触摸服务器?

转载 作者:行者123 更新时间:2023-11-28 01:42:20 24 4
gpt4 key购买 nike

我有一个连接到 dijix.TreeJsonRest。节点是延迟加载的。这就是我的树的样子:

var store = new Observable(new JsonRest({
target: "...",
idProperty: "id",
mayHaveChildren: function(object) {
return object.hasChildren;
},
getChildren: function(object, onComplete, onError) {
this.get(object.id).then(function(fullObject) {
object.children = fullObject.children;
onComplete(fullObject.children);
}, onError);
},
getRoot: function(onItem, onError) {
this.get("I1").then(onItem, onError);
},
getLabel: function(object) {
return object.name;
}
}));

第一次伸展树(Splay Tree)节点时,请求被发送到服务器以获取该节点的子节点。

但是,此调用已缓存,因此下次我展开节点时,不会发送任何请求。我还注意到 getChildren 函数仅在第一次展开时调用一次。

我想要实现的是禁用缓存,即。 每次展开节点时发送请求

最佳答案

事实上,这是可能的。分析 Dojo 的 Tree.js 代码,我得出的结论是,每个节点展开时调用的函数是

_expandNode: function(/*TreeNode*/ node)

该函数包含以下代码:

// Load data if it's not already loaded
if(!node._loadDeferred){
// load the node
}

当然,我们的罪魁祸首是 _loadDeferred 属性。那么问题来了,如何为每个展开的节点设置这个属性。

我决定在 getChildren 方法中执行此操作。首先,我需要根据商店中的商品获取树节点,然后我必须将其属性设置为 false。

  1. 我发现 tree.getNodesByItem(item) 对第一部分起作用。唯一要记住的是它返回一个节点数组,因此我们需要获取第一个元素:

    tree.getNodesByItem(item)[0]
  2. 现在只需设置属性即可:

    tree.getNodesByItem(item)[0]._loadDeferred = false;

所以,最终解决方案

var store = new Observable(new JsonRest({
target: "...",
idProperty: "id",
mayHaveChildren: function(object) {
return object.hasChildren;
},
getChildren: function(object, onComplete, onError) {
this.get(object.id).then(function(fullObject) {
object.children = fullObject.children;
onComplete(fullObject.children);
tree.getNodesByItem(object)[0]._loadDeferred = false;
}, onError);
},
getRoot: function(onItem, onError) {
this.get("I1").then(onItem, onError);
},
getLabel: function(object) {
return object.name;
}
}));

关于javascript - 如何禁用 Dojo 树中每个节点扩展上的缓存/触摸服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20761355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com