On 01/30/2013 11:36 AM, Daniel Renfro wrote:
We're finding that when using jQuery's .ready() (or similar) function, the callbacks seem to execute in different (unexepected, browser-dependent) order. This causes errors.
jQuery will execute ready events in the order bound. See e.g. http://stackoverflow.com/questions/3934570/order-of-execution-of-jquery-docu... (the mechanism in latest jQuery is now a Promise, but it's still in order).
So if the ResourceLoader dependency chain is strictly right, it should execute in order. If not, it's a bug in either jQuery or ResourceLoader.
Using the WikiEditor extension as a specific example: Customizing the WikiEditor-toolbar is one of the specific cases where we've encountered problems. First, the WikiEditor provides no good events to bind to once the toolbar is loaded.
I just added this. The event is attached to the main textbox used by the toolbar (same as the previous custom events). So to listen, do:
$( '#wpTextbox1' ).on( 'wikiEditor-toolbar-doneInitialSections', function () {
} );
WikiEditor is using eachAsync, which uses setTimeout internally. Therefore, even if a ready event is bound afterwards, it's not enough. Hence, the new event.
My questions: It recently dawned on me that executing our code within a $(document).ready(); callback might not be necessary as the JavaScript for each ResourceLoader module is executed in its own callback on the client-side.
Only in production mode, so don't rely on it. You can do your own immediately executing anonymous function like https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript#Closure
Is it a good idea to avoid binding our code to jQuery's ready event?
If you're not sure if your code is running after ready, bind with $(document).ready . It will be immediately executed, so you're not losing much.
Matt Flaschen