我从一块一块的代码开始,以便问题描述变得清晰。
我有一段HTML代码:
<div id="center" class="column" dojoType="dijit.layout.TabContainer">
<div id="first" dojoType="dijit.layout.ContentPane" title="first" selected="true">
<div id="first_content"></div>
</div>
<div id="second" dojoType="dijit.layout.ContentPane" title="second">
<div id="second_content"></div>
</div>
</div>
我有一个javascript函数将dijit树加载到HTML中:
function load()
{
//load data
dojo.xhrGet(firsthierarchy("first_content", "file1.json"));
dojo.xhrGet(secondhierarchy("second_content", "file2.json"));
}
function firsthierarchy(node, url){
return {
url: url,
node: dojo.byId(node),
handleAs: "json",
load: loadfirsthierarchy
};
}
function secondhierarchy(node, url){
return {
url: url,
node: dojo.byId(node),
handleAs: "json",
load: loadsecondhierarchy
};
}
function loadfirsthierarchy(data, xhr)
{
if(xhr.args.node)
{
store1 = new dojo.data.ItemFileWriteStore({data: data});
treeModel1 = new dijit.tree.ForestStoreModel({
store: store1,
query: {
"type": "continent"
},
rootId: "root",
rootLabel: "Continents",
childrenAttrs: ["children"]
});
tree1 = new dijit.Tree({
model: treeModel1,
},xhr.args.node.id);
}
}
function loadsecondhierarchy(data, xhr)
{
if(xhr.args.node)
{
store2 = new dojo.data.ItemFileWriteStore({data: data});
treeModel2 = new dijit.tree.ForestStoreModel({
store: store2,
query: {
"type": "continent"
},
rootId: "root",
rootLabel: "Continents",
childrenAttrs: ["children"]
});
tree2 = new dijit.Tree({
model: treeModel2,
},xhr.args.node.id);
}
}
以上所有功能都运行良好。现在,我希望有一个重置函数,它可以消除“first_content”和“second_content”div中的现有树,并使用具有新内容的新树加载这些div。例如:
function reset()
{
// here I want to completely wipe out the exiting trees in all of the dojo contentpanes.
// And I want to load the contentpanes with entire new set of data.
// Maybe like :
// dojo.xhrGet(firsthierarchy("first_content", "file3.json"));
// dojo.xhrGet(secondhierarchy("first_content", "file4.json"));
}
我不知道如何实现重置功能。能不能给我一个线索。
答案 0 :(得分:0)
保持对树,treeModel和store的引用,然后将您的函数定义为:
reset: function() {
myTree.destroyRecursive();
myTreeModel.destroyRecursive(); // I'm not 100% sure you need this, destroying the tree may do this automatically
delete myStore
}
应该做的伎俩
我这样做的方法是使用名为TreeWrapper()
的dojo.provide创建一个模块,它处理树生命周期(获取数据,创建树,杀死树)。然后,此包装器模块具有上面的重置功能,并且可以包含对所有不同变量的引用。
您还需要跟踪您对树的所有dojo.subscribe
并取消订阅。