dojo.parser.parse并不总是返回 [英] dojo.parser.parse doesn't always return

查看:368
本文介绍了dojo.parser.parse并不总是返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码。它用于在select元素更改后更新表单。 onChange一个ajax调用,这一段代码照顾了响应。



第一次一切都按预期工作。但是,dojo.parser.parse无法返回约50%的时间。



起初它看起来像这样:

  var targetNode = dojo.byId(node); 
targetNode.innerHTML = data;
dojo.parser.parse(targetNode);

然后我读了一些关于现有的对象。所以我以为可能会摧毁他们将有助于:

  if(dojo.byId(node))dojo.destroy(node); 
dojo.create('div',{id:node},afternode,'after');
var targetNode = dojo.byId(node);
targetNode.innerHTML = data;
dojo.parser.parse(targetNode);

没有任何帮助。这是怎么回事?有时它会解析一些元素。这是dojo.parser的一个已知问题吗?

解决方案

如果您以声明方式创建dijits并使用 dojo.parser.parse 可以即时解析它们,并指定dijit的ID,一旦解析了相同的HTML片段两次,您会得到一个错误,说dijit的ID已被注册。

 < div dojoType =dijit.form.Buttonid =myButton/> 

原因是dijits尚未被销毁,您无法重新使用ID。如果您在声明时未指定ID,则不会收到此错误,但实际上会有内存泄漏。



正确的方法是在再次解析HTML片段之前销毁dijits。 dijit.parser.parse 的返回值是一个数组列表,其中包含从HTML片段解析的所有dijits的引用。您可以保留列表并首先销毁dijits。

  if(dijits){
for(var i = 0 ,n = dijits.length; i< n; i ++){
dijits [i] .destroyRecursive();
}
}
dijits = dojo.parser.parse(targetNode);


I have this bit of code. It is used to update the form after a select element changes. onChange an "ajax" call is made and this bit of code takes care of the response.

The first time everything works as expected. However it the dojo.parser.parse fails to return about 50% of the time.

At first it looked like this:

var targetNode = dojo.byId(node);
targetNode.innerHTML = data;
dojo.parser.parse(targetNode);

Then I read something about the objects existing. So I thought that maybe destroying them would help:

if(dojo.byId(node)) dojo.destroy(node);
dojo.create('div', { id: node }, afternode, 'after');
var targetNode = dojo.byId(node);
targetNode.innerHTML = data;
dojo.parser.parse(targetNode);

That didn't help any. What the h3ll is going on? Sometimes it parses some of the elements. Is this a known problem with the dojo.parser?

解决方案

If you create the dijits declaratively and use dojo.parser.parse to parse them on-the-fly and you specify the ID of the dijit, once you parse the same HTML fragment twice, you'll get an error says that the dijit's ID has been registered.

<div dojoType="dijit.form.Button" id="myButton" />

The cause is that the dijits have not been destroyed yet and you can not reuse the ID. If you don't specify the ID when declaring it, you won't get this error, but you actually have memory leaks.

The correct way is to destroy the dijits before parsing the HTML fragment again. The return value of dijit.parser.parse is an array list that contains the references of all the dijits it parsed from the HTML fragment. You can keep the list and destroy the dijits first.

if (dijits) {
  for (var i = 0, n = dijits.length; i < n; i++) {
      dijits[i].destroyRecursive();
  }
}
dijits = dojo.parser.parse(targetNode);

这篇关于dojo.parser.parse并不总是返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆