dojo.parser.parse only working first time it's called
I have a page that when a user clicks on a link for some reporting tools, it first asks them to enter some report parameters. I get the parameters dialog as a form using AJAX, based on the id of the link. Each dialog has some dojo controls on it, so I need to parse them when the 开发者_StackOverflow社区dialog appears, because it is not originally part of the page.
The first dialog when called works fine, but subsequent calls to dialogs fails to parse the dojo controls.
Example:
showParametersDialog : function(doc) {
var content = doc.firstChild.firstChild.data;
var container = document.createElement('div');
container.id = 'dialog';
container.innerHTML = content;
container.style.background = 'transparent';
container.style.position = 'absolute';
container.style.top = (document.body.clientHeight / 2) - 124 + "px";
container.style.left = (document.body.clientWidth / 2) - 133 + "px";
container.style.display = 'block';
document.body.appendChild(container);
// set up date fields
var date_from = dojo.byId('date_from');
var date_to = dojo.byId('date_to');
try {
date_from.value = dojo.date.locale.format(new Date(), {selector: 'date'});
date_to.value = dojo.date.locale.format(new Date(), {selector: 'date'});
} catch(e) {
var now = new Date();
date_from.value = String(now.getMonth() + "/" + now.getDate() + "/" + now.getFullYear());
date_to.value = String(now.getMonth() + "/" + now.getDate() + "/" + now.getFullYear());
}
dojo.parser.parse();
}
All dialogs have the common date fields. So when I call this dialog the first time, and dojo.parser.parse() is called, it parses the controls on the dialog, but only the first time...after than, no dojo.
Any thoughts?
Thanks, Paul.
You can use
dojo.parser.instantiate([dojo.byId("myDiv")]);
instead of dojo.parser.parse(); Use it on all unparsed objects.
More on this you can find here:
http://livedocs.dojotoolkit.org/dojo/parser
it's not that it's not parsing the second time, it's that if dojo tries to parse something more than once, it fails. You could keep track with a boolean flag whether it has been parsed already, and if not, skip the line that parses it.
if (!parsed)
{
dojo.parser.parse();
}
This might be because you're missing something like dojo.parser.parse(container)
. Otherwise, it probably tries to parse the entire document and finds parsed elements and stops.
(wild guess pulled from old dojo knowledge)
精彩评论