Reload dojo widgets after ajax request

℡╲_俬逩灬. 提交于 2019-12-04 07:56:10

This is because the widgets in the HTML fragment returned from server needs to be parsed first before it can be shown in the page. In your main page, you probably have code like below:

<script type="text/javascript" src="../javascripts/dojo1.6/dojo/dojo.js"
djConfig="parseOnLoad: true"></script> 

The configuration parseOnLoad: true means dojo parses the entire page automatically after the page is loaded. This is not true when you use XHR to get another HTML fragment from the server. You need to parse the widgets manually in this case.

Use dojo.parse.parse function to parse a DOM node tree. So what you need to do is to invoke this function after the HTML fragment is added to the page. For example,

$('#ajax-content').html(html);    
dojo.parser.parse(dojo.byId('ajax-content'));

One thing you need to note is to make sure the widgets have been destroyed before the page is updated again to avoid memory leak. dojo.parser.parse function returns an array of all the parsed widgets' object. So before the page is updated again, you can iterate this array and destroy these widgets.

//Save the last parsed result
var widgets = dojo.parse.parse();

//Before update
if (widgets) {
    widgets.forEach(function(widget) {
        widget.destroyRecursive();
    });
} 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!