Skip to content

Instantly share code, notes, and snippets.

@jonnyreeves
Created June 12, 2012 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnyreeves/2917860 to your computer and use it in GitHub Desktop.
Save jonnyreeves/2917860 to your computer and use it in GitHub Desktop.
RequireJS Loading order
// This doesn't appear to be the case...
define(function (require) {
// Is there any guarantee that `moduleA` is loaded first?
var loadMeFirst = require('moduleA');
var loadMeSecond = require('moduleB');
});
// In this case `moduleA` *appears* to always be loaded before `moduleB`
define([ 'moduleA', 'moduleB' ], function (loadMeFirst, loadMeSecond) {
});
@jrburke
Copy link

jrburke commented Jun 12, 2012

There is no guarantee of load order with either approach. All scripts are loaded async and only guarantee is that the factory functions above are not called until both dependencies have been loaded and evaluated.

@jonnyreeves
Copy link
Author

Thanks for clarifying that, James. I ended up with this approach which seems to do the job - could you confirm it's the 'correct' way to handle ordered dependencies?

// Application main entry point definition.  Load any configuration options which relate to the framework actors
// before loading the frameworks themselves.
define(['logger-config', 'jquery-mobile-config'], function () {

    // Logger and jQueryMobile configs have been loaded, now for the app...
    require(['songbook/app', 'jquery-mobile'], function(app) {

        // jQuery-Mobile and app are ready to go!
        app.init();
    }); 
});

@jrburke
Copy link

jrburke commented Jun 12, 2012

If you are using requirejs 2.0, the shim config is helpful for this kind of situation where you have implicit dependencies (the library does not call define()) but want to use it as a module:

http://requirejs.org/docs/api.html#config-shim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment