简体   繁体   English

dojo:通过dom节点销毁所有小部件

[英]dojo: destroy all widgets by dom node

My content is replaced with ajax but sometimes an element will have the same id on two pages (ie, a photo on the home page has the same id on the gallery page). 我的内容被替换为ajax但有时一个元素在两个页面上具有相同的id(即,主页上的照片在gallery页面上具有相同的id)。 This means that when dojo.parser.parse is called, the widgets are trying to be re-added, and the below error is thrown: 这意味着当调用dojo.parser.parse时,小部件会尝试重新添加,并抛出以下错误:

Error: Tried to register widget with id==____ but that id is already registered

Ideally, what I'd like to do is run destroyRecursive on the DOM node that the AJAX replaces. 理想情况下,我想要做的是在AJAX替换的DOM节点上运行destroyRecursive。 I've tried both of the below but neither work (I believe destroyRecursive is for widgets not DOM?): 我已经尝试了以下两种方法,但都没有工作(我相信destroyRecursive是针对小部件而不是DOM?):

dojo.byId('main').destroyRecursive();
dijit.byId('main').destroyRecursive();

Is there a good way of doing this, or do I need to try and ensure that all my id's are different? 有没有一种很好的方法可以做到这一点,还是我需要尝试确保我的所有ID都不同?

You are on the right track, and you are correct that destroyRecursive only exists on widgets. 你是在正确的轨道上,你是正确的,destroyRecursive只存在于小部件上。 However, there are a couple of choices to accomplish what you want to do. 但是,有几种选择可以完成您想要做的事情。

If you're using widgets to a significant extent, and the div in question is regularly being used as a bucket to hold content including widgets, then I would highly recommend you have a look at dijit.layout.ContentPane . 如果您在很大程度上使用小部件,并且有问题的div经常被用作存储包含小部件的内容的存储桶,那么我强烈建议您查看dijit.layout.ContentPane ContentPane is a widget primarily focused around the idea of a container that receives content, either directly or from a URL, which may or may not include widgets. ContentPane是一个小部件,主要关注容器的概念,该容器直接或从URL接收内容,URL可能包含也可能不包含小部件。

Right now you're probably doing something like this on each page change: 现在你可能在每个页面更改时做这样的事情:

dojo.xhrGet({
    url: 'something.html',
    load: function(html) {
        dojo.byId('main').innerHTML = html;
        dojo.parser.parse(dojo.byId('main'));
    }
    error: function(error) { ... }
});

With a ContentPane, you could do the same thing like this: 使用ContentPane,您可以执行以下相同的操作:

cp.set('href', 'something.html'); //use attr instead of set if < dojo 1.5

With this, ContentPane will not only fetch that URL and hold its contents - it will also parse any widgets within it - and equally importantly, it will automatically destroy any existing widgets within itself before it replaces its content. 有了这个,ContentPane将不仅获取该URL并保存其内容 - 它还将解析其中的任何小部件 - 同样重要的是,它将在替换其内容之前自动销毁其自身内的任何现有小部件。

You can read more about it in the Dojo documentation: 您可以在Dojo文档中阅读有关它的更多信息:

Alternatively, if you don't feel like using a widget to hold your content, you can look for widgets in your div and destroy them yourself. 或者,如果您不想使用小部件来保存您的内容,您可以在div中查找小部件并自行销毁它们。 Here's the easiest way to do it: 这是最简单的方法:

dojo.forEach(dijit.findWidgets(dojo.byId('main')), function(w) {
    w.destroyRecursive();
});
dojo.query('selector').forEach(function(node){
   dijit.byNode(node).destroyRecursive(true);
});

Basically, selecting the node... You can get the mapped as widget object by using dojo.byNode(node) , and then destroyRecursive(true); 基本上,选择节点...您可以使用dojo.byNode(node)获取映射为widget对象,然后使用destroyRecursive(true);

I solved a similar problem, simply deleting from registry using dijit.registry.remove('idName') after eliminating the content with destroyRecursive(false) , before Reloading it. 我解决了类似的问题,只需在重新加载之前使用destroyRecursive(false)删除内容后,使用dijit.registry.remove('idName')从注册表中删除。

    if(typeof registry.byId("tableOfContents") != "undefined"){
        registry.byId("tableOfContents").destroyRecursive(false);
        dijit.registry.remove('tableOfContents');
    }

If you have more than one widget to be destroyed on a page, the following solution works for me. 如果您要在页面上销毁多个小部件,则以下解决方案适用于我。

var widg = dijit.findWidgets(dojo.byId('root-id')); // root-id is top div id which encloses all widgets
 $(widg).each(function(){

    dijit.byId($(this).attr("id")).destroy(true);

  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM