PDA

View Full Version : Ext.onReady help



jjathman
15 Apr 2010, 10:29 AM
I tried searching for a similar problem but I didn't see anything. I apologize if this is a known issue.

We have some code that is executed via Ext.onReady which in turn also calls an Ext.onReady piece of code. Prior to Ext 3.x this code worked OK, but is now leading to an infinite recursion. Here is a really simplified case:



function bar(){
console.debug("in bar");
}

function foo(){
console.debug("in foo");
Ext.onReady(bar);
console.debug("done with foo");
}

Ext.onReady(foo);
With Ext 2.2 for example, this will give you:
in foo
done with foo
in bar

But with Ext 3.2 final, this will give you:
in foo
in foo
in foo
...

forever until you get a too much recursion error (at least in Firefox). Were we exploiting a behavior in Ext 2.2 that we shouldn't have been? It doesn't seem like we are doing anything obviously wrong.

rbastic
19 Apr 2010, 6:48 AM
#1: Stop.

#2: Stop.

#3: Stop.

Don't do it this way. Ext.onReady() is supposed to be called ONCE. When the script is loaded and ExtJS is ready. That's IT.

jjathman
19 Apr 2010, 8:04 AM
Although I think in an ideal world only calling onReady once would be ideal, I don't think this is really always practical. We have various parts of our application that are designed to be independent of each other and not all of our JavaScript code is always included on every page, so it is not possible for us to have only one function called onReady. In my trivial example it would be easy to refactor this to only have one function get called onReady, but in a larger app I don't think this is easy to do.

Now if the way we are using onReady is not appropriate I think the docs should at least mention that. The docs make it sound like this is a safe way to add listeners to the dom being ready. Also this functionality we were using before worked ok in Ext 2.x but does not work in 3.2.

rbastic
19 Apr 2010, 12:09 PM
The world isn't ideal.

If you look at ExtJS' code:




onDocumentReady : function(fn, scope, options){
if(Ext.isReady){ // if it already fired or document.body is present
docReadyEvent || (docReadyEvent = new Ext.util.Event());
docReadyEvent.addListener(fn, scope, options);
docReadyEvent.fire();
docReadyEvent.listeners = [];
}else{
if(!docReadyEvent){
initDocReady();
}
options = options || {};
options.delay = options.delay || 1;
docReadyEvent.addListener(fn, scope, options);
}
},


Ext.isReady gets set elsewhere in the code when the document ready state changes. ... So, it only fires once.

I'm not an ExtJS guru, so I don't know why it worked in 2.2x. Either way -- surely, there must be another way to implement what you want? What if you directly call docReadyEvent.addListener(yourFunction) over and over again as necessary, rather than trying to use Ext.onReady() multiple times.



-Ryan