Search code examples
javascriptjquerydomextjsextjs4

Ext.onReady() vs $(document).ready()


Whats the difference? I have on $(document).ready function which should check if extjs is loaded but the main problem is extjs does not load on time and things inside $(document).ready starts to execute, extjs create function which produces the main error 'cannot execute create of undefined' on Ext.create("...", {..}); line. If i put double check like this:

$(document).ready(function() {
    Ext.onReady(function() {
        Ext.create('Ext.Button', {...});
    });
});

Things magically work. Now I'm using ext-all.js which has ~1.3MB minified which is pretty large imho...and things get magically loaded while he does the second check...but I think those 2 functions are not the same as they definitions suggest, because if I put another $(document).ready instead of Ext.onReady() line, things break again. I think Ext.onReady({}); function does some other black magic which $(document).ready() does not, and I'm interested if someone knows what is this kind of magic?

Because it work's and I don't know why which is killing me.

Thanks for reading the post. =) ps. I'm using ExtJS for about day so I'm pretty new to it.


Solution

  • No they're not the same, the first one will proc when your jQuery library is loaded, the Ext.onReady(.. will proc when your ExtJS library is loaded.

    If you want to combine them you could do something like this:

    var extReady = false;
    var jQueryReady = false;
    
    var librariesReady = function () {
        if (jQueryReady && extReady) {
            //They're both ready
        }
    };
    
    $(document).ready(function () {
        jQueryReady = true;
        librariesReady();    
    });
    Ext.onReady(function () {
        extReady = true;
        librariesReady();
    });