View Full Version : JS from XHR execute after onUpdate in UpdateManager?

6 Nov 2006, 6:49 PM

In this post: http://jackslocum.com/forum/viewtopic.php?t=537

You pointed out I should manually load Example.init because no load event will fired for XHR. But I have a lot of similar grid to be load in XHR, it isn't possible to trigger every init manually. So I'm thinking about add(a register function) each XHRed JS object(say "Example" in the post) to a array "xhr_init" in main page and add a callback of onUpdate event in main page to activate each "init" function in the "xhr_init"(of course after reigsteration).

The problem is the register function of each XHRed JS object is called after onUpdate fired, so there is nothing can be inited, a manual call of init function on the registered object is working fine after the page loaded.

Another solution is add a "last-load-div" to the end of XHRed content and then attach onVailable to the init function(working fine), but I think it isn't so reasonable compared with the one I'm having trouble with.

7 Nov 2006, 1:55 AM
<span class="harping-on-again">This seems to be the problem of waiting for all scripts from the loaded content to be registered....<span>

To make the onUpdate event fire AFTER rendering, try adding this code somewhere after yui-ext.js has been loaded:

YAHOO.ext.UpdateManager.prototype.processSuccess = function(response){
this.transaction = null;

// The callback function is called by the renderer.
this.renderer.render(this.el, response, this, function()
this.onUpdate.fireDirect(this.el, response);
if(typeof response.argument.callback == 'function'){
response.argument.callback(this.el, true);
if(response.argument.form && response.argument.reset){
try{ // put in try/catch since some older FF releases had problems with this

YAHOO.ext.UpdateManager.BasicRenderer.prototype.render = function(el, response, updateManager, callback){
// The callback function is called by Element.update() after the DOM has been updated
el.update(response.responseText, updateManager.loadScripts, callback);

YAHOO.ext.Element.prototype.update = function(html, loadScripts, callback)
var dom = this.dom;

// Id of additional element added at the end of the innerHTML so that we
// can wait for its presence
var waitForId = YAHOO.util.Dom.generateId();
var totalWait = 0;

var postProcessor = function(){
// If we've been waiting less than half a second, and renderer has not caught up, wait again
if ((totalWait < 500) && (dom.lastChild.id !== waitForId))
totalWait += 10;
setTimeout(postProcessor, 10);

if (loadScripts)
var s = dom.getElementsByTagName("script");
var docHead = document.getElementsByTagName("head")[0];

// For browsers which discard scripts when inserting innerHTML, extract the scripts using a RegExp
if(s.length == 0){
var re = /(?:<script.*(?:src=[\"\'](.*)[\"\']).*>.*<\/script>)|(?:<script.*>([\S\s]*?)<\/script>)/ig; // assumes HTML well formed and then loop through it.
var match;
while(match = re.exec(html)){
var s0 = document.createElement("script");
if (match[1])
s0.src = match[1];
else if (match[2])
if (YAHOO.util.Dom.isSafari)
s0.innerHTML = match[2];
s0.text = match[2];
}else {
for(var i = 0; i < s.length; i++){
var s0 = document.createElement("script");
s0.type = s[i].type;
if (s[i].text) {
if (YAHOO.util.Dom.isSafari)
s0.innerHTML = s[i].text;
s0.text = s[i].text;
} else {
s0.src = s[i].src;

// Callback
if(typeof callback == 'function'){
callback(this.dom, true);
delete postProcessor;

dom.innerHTML = html + "<span style=\"display:none\" id=\"" + waitForId + "\"></span>";

// Post process - this waits for the renderer to render element 'waitForId'

7 Nov 2006, 5:01 AM
Many thanks for your patch, it works. Why isn't it included in the latest beta? Is there any side-effect or any possibility to break the other part of the YAHOO.ext code ?

7 Nov 2006, 5:57 AM
I discussed this with Jack (He's probably bored of the issue by now) here: http://www.jackslocum.com/forum/viewtopic.php?t=166

IMHO, onUpdate should fire when updating is complete - as in my patch. That patch is the code I use to

* Enable Ajax-loaded HTML to contain scripts which will all be processed.
* Enable postprocessing of Ajax-loaded HTML in the knowledge that it's all there.

7 Nov 2006, 3:10 PM
Animal, I'm willing to revisit it. It seems to be popping up a lot.