Don’t declare a variable called args in Ext.onReady function


Recently, I was trying the following:

Ext.onReady(function() {
    var args = someMethod(),
        v = args.v;
    if (v) {
        ...
    }
    ...
}

Quite strangely, the v variable was always getting the undefined value.

The reason is in fact very simple: Ext.onReady is using the createDelayed function. This method creates a function that will execute another in a given context after a specified time. It uses setTimeout to schedule the execution of a function that invokes the specified function in the specified scope. The catch is: the created function has a closure which contains a variable called args (used to copy the supplied arguments).

So the motto: no args variable in anything called from createDelayed(f, o, scope).
That is any listener that has a delay specified. And that includes Ext.EventManager.onDocumentReady and its shortcut version Ext.onReady (if no delay is specified, a delay of 1ms is imposed).

Share

Les commentaires sont fermés.