peirix
peirix

Reputation: 37771

parsing dojoType on AJAX loaded content

I'm getting an error when parsing checkboxes in a table that is loaded with AJAX, but I get an error saying the widget with that id is already registered:
"Error('Tried to register widget with id==userListUncheckAll but that id is already registered')"

And I'm guessing this happens because we take out the current table, then replace it with what ever we get back from the AJAX call, and thus the element id's would be the same. Is there a way to "unregister" the widgets or something similar?

Upvotes: 4

Views: 2931

Answers (3)

Alfredo Carrillo
Alfredo Carrillo

Reputation: 316

Within the Dojo Parser documentation is included this code snipet:

dojo.xhrGet({
  url: "widgets.html",
  load: function(data){
    dojo.byId("container").innerHTML = data;
    dojo.parser.parse("container");
  }
})

I applied in my code and works fine.

Upvotes: 0

Josh G
Josh G

Reputation: 14256

Get the parent node that you are inserting the AJAX content into and parse ONLY this node. You are getting this error because other widgets in your DOM are getting parsed twice. Something like this:

require(["dojo/dom", "dojo/parser", "dojo/_base/xhr"/*, etc */ ], 
   function(dom, parser, xhr) {
      var request = xhr.get({
         // your details here
      });

      request.then(function(data) {
         // transform data if necessary
         var parentNode = dom.byId("/* parent id */");
         parentNode.innerHTML = data;

         // This is where the widgets get built!
         parser.parse(parentNode); // or parser.parse("/* parent id */");
      }, function(err) {
         // handle error
      });
});

Also, make sure you include the right dojo / dijit modules. A common mistake is to forget to include the modules for the widgets that you are trying to insert. For example, if you are using TabContainer, add "dijit/layout/TabContainer" to the list of required modules.

Upvotes: 1

peirix
peirix

Reputation: 37771

I found the answer for this myself, so I'll put it here for others:

If you have a set of id's that you know will need to be "unregistered", create an array of the id names:

try {
    dojo.parser.parse();
} catch (e) {
    var ids = ['id1', 'id2', 'id3'];
    dijit.registry.forEach(function(widget) {
        //remove this check if you want to unregister all widgets
        if (dojo.indexOf(ids, id) { 
            widget.destroyRecursive();
        }
    });
    dojo.parser.parse();
}

Works like a charm.

Upvotes: 4

Related Questions