Here is my main.js before using domReady:
require.config({ paths : { loader : 'libs/backbone/loader', jQuery : 'libs/jquery/jquery-module', Underscore : 'libs/underscore/underscore-module', Backbone : 'libs/backbone/backbone-module', templates : '../Templates' } }); require([ 'app' ], function(app) { app.initialize(); });
And app.js:
define([ 'jQuery', 'Underscore', 'Backbone', 'router', 'services/Initializers/MainFrameInitializer', 'services/Initializers/FlowsViewsInitializer', 'services/Initializers/EditModuleInitializer', 'services/Sandboxes/ModulesNavigationSandbox', 'services/Sandboxes/ApplicationStateSandbox', 'DataModel/Constants' ], function($, _, Backbone, Router, MainFrameInitializer, FlowsViewsInitializer, EditModuleInitializer, ModulesNavigationSandbox, ApplicationStateSandbox, Constants) { var initialize = function() {
Everything works fine until I optimize the project. I found out that the script starts working before the DOM is ready, something that was wrong before the optimization. Anyway, I want to use the domReady plugin to make sure the DOM is loaded first.
But apparently I have no idea how to do it right. Here is the new version of main.js:
require.config({ paths : { loader : 'libs/backbone/loader', jQuery : 'libs/jquery/jquery-module', Underscore : 'libs/underscore/underscore-module', Backbone : 'libs/backbone/backbone-module', templates : '../Templates' } }); require([ 'domReady', 'app' ], function(domReady, app) { domReady(app.initialize); });
Very neat and very wrong, because the app loads in parallel with domReady until the DOM is ready.
How to fix it?
Thanks.
EDIT
I think I understood our problem. The app dependency constructor functions should not run any DOM-dependent code. They should simply return functions, capturing DOM-dependent logic. This logic must be executed from app.initialize , which is guaranteed to be executed when the DOM is ready.