views:

367

answers:

2

Hi,

I am new to the Dojo Toolkit. I'm getting the error

Tried to register widget with id=myButton but that id is already registered

whenever I try to load dojo content twice (meaning I load HTML content through jQuery.Load into a container div). Is there a way of unregistering already registered widgets in dojo? I've seen some examples, but I don't really get them working.

My button:

<button dojoType="dijit.form.Button" id="myButton">button</button>
+1  A: 

Based on http://bugs.dojotoolkit.org/ticket/5438, I found a sufficient way of destroying dojo-widgets:

dijit.registry.forEach(function(w){
                  w.destroy();             
          });
John Korsnes
+3  A: 

If you're looking to unregister specific widgets, you can use their destroy() or destroyRecursive() methods. The second one destroys any widgets inside the one you are destroying (i.e. calling destroyRecursive on a form widget will also destroy all the form components).

In your case, it sounds like your best bet would be to do this before jQuery.load -

var widgets = dijit.findWidgets(<containerDiv>);
dojo.forEach(widgets, function(w) {
    w.destroyRecursive(true);
});

The above code will unregister all widgets in <containerDiv>, and destroy their associated DOM Nodes. To preserve the DOM nodes, pass false to destroyRecursive instead.

Jez
Others looking for a solution : See also answer below for non-recursive solution.
John Korsnes