How to unregister or re-register a widget in a dojo javascript
I have an application that is two combo boxes. Depending on the value in the first combo box, the second box auto-populates with some data. This works fine if the user only selects one of the three values in the first box(Skill, Tool, Client) , but if the user selects one value, then changes it back to the original value,i get a message like so:
Tried to register widget with id==expandableQueryList0.skill.name but that id is already registered
The problem is, i need to be able to find these values later for the purposes of building a query, and I am already dynamically building the combo boxes, and in this example expandableQueryList0.skill.name was the dynamically generated name. If they selected tool, it would get expandableQueryList0.tool.name and then changing the combobox back to Skill (and this the 2nd widgets ID to expandableQueryList0.skill.name) is what gives me the error.
behold the code:
function setVal(value, table, paramCount, offset) {
if(((value == 'Skills') || (value == 'Tools')|| (value == 'Clients') ) ){
var row_template = "";
if(value == 'Skills'){
row_template = $('skillbox_template');
}else if(value == 'Tools'){
row_template = $('toolbox_template');
}else{
row_template = $('clientbox_template');
}
var t = new Template(row_template.text);
var evaledTemplate = t.evaluate({qIdx:paramCount});
var valID = "expandableQueryList[" + (paramCount) + "].valBox";//sets the开发者_开发技巧 valID
var widgets = dijit.findWidgets(valID);
dojo.forEach(widgets, function(w) {
w.destroyRecursive(true);
});
var ni = document.getElementById(valID);//finds the valID in the evaledTemplate
//create a new div to hold the new search box
var divIdName = "my"+(paramCount)+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = evaledTemplate;
dojo.empty(ni);
ni.appendChild(newdiv);
dojo.parser.parse(newdiv);
}
}
So, as you can see, i Have some attempt at clearing out the DIV that holds the combobox in question. This function is called onChange
What can I do to solve this problem? Is there a way to check to see if a widget is already registered by ID, and then just point to that one again instead of making a new one?
You can use dijit.byId()
to check whether a widget exists with the ID. For example:
if (!dijit.byId(divIdName)) {
//create the widget.
}
The return value of dojo.parser.parse()
is an array of all the widgets that created in the parsing. So you can keep a reference of the widget that created and use that reference to check. For example:
var widgets = {};
function setVal(value, table, paramCount, offset) {
if (widgets[paramCount]) {
//skip the creation
}
else {
//create the widget
widgets[paramCount] = dojo.parser.parse(newdiv)[0];
}
}
精彩评论