【问题标题】:Completely wipe out the dijit (dojo) tree from memory and free its placeholder从内存中彻底清除 dijit (dojo) 树并释放其占位符
【发布时间】:2011-03-06 11:20:37
【问题描述】:

我从一段一段代码开始,以便问题描述变得清晰。

  1. 我有一段 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>
    

    &lt;/div&gt;

  2. 我有一个 javascript 函数可以将 dijit 树加载到 HTML 中:

函数加载()

{
//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。例如:

函数重置()

{
// 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"));
}

我不知道如何实现重置功能。你能不能给我一个线索。

【问题讨论】:

    标签: html tree dojo


    【解决方案1】:

    保留对树、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 并取消订阅这些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2014-10-06
      • 1970-01-01
      • 2013-10-05
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多