Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load files in specific order with RequireJs

Tags:

I'm new to RequireJS and I'm stuck with the loading order.

I have a global project configuration that I need to be loaded before the modules located in js/app/*.

Here's my struture :

index.html config.js js/     require.js     app/         login.js     lib/         bootstrap-2.0.4.min.js 

Here's the config.js file :

var Project = {     'server': {         'host': '127.0.0.1',         'port': 8080     },     'history': 10,      // Number of query kept in the local storage history     'lang': 'en',       // For future use }; 

And here's my requirejs file (app.js) :

requirejs.config({     //By default load any module IDs from js/lib     baseUrl: 'js/lib',     //except, if the module ID starts with "app",     //load it from the js/app directory. paths     //config is relative to the baseUrl, and     //never includes a ".js" extension since     //the paths config could be for a directory.     paths: {         bootstrap: '../lib/bootstrap-2.0.4.min',         app: '../app',     },     shim: {         'app': {             deps: ['../../config'],             exports: function (a) {                 console.log ('loaded!');                 console.log (a);             }         } // Skual Config     }, });  var modules = []; modules.push('jquery'); modules.push('bootstrap'); modules.push('app/login');   // Start the main app logic. requirejs(modules, function ($) {}); 

But sometimes, when I load the page, I have a "Project" is undefined, because login.js has been loaded BEFORE config.js.

How can I force config.js to be loaded at first, no matter what ?

Note: I saw order.js as a plugin for RequireJS but it's apparently not supported since the v2, replaced by shim.

like image 387
Cyril N. Avatar asked Jul 20 '12 14:07

Cyril N.


People also ask

Is RequireJS still relevant?

RequireJS is, in web-terms, an old technology now (some might say ancient), but it is still in wide use and there have been a number of questions about RequireJS and DataTables recently.

Is RequireJS synchronous?

Is RequireJS synchronous? So, RequireJS doesn't support it. From your use case it seems that you don't need synchronous RequireJS, you need to return result asynchronously. AMD pattern allows to define dependencies and load them asynchronously, but module's factory function must return result synchronously.

What is RequireJS Shim?

As per RequireJS API documentation, shim lets you. Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. - Configuring dependencies.

What is require () in JavaScript?

require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.


1 Answers

Ran into a similar problem today - we have bootstrapped data that we want to make sure is loaded before anything else, and that the module exposing that data is set up before any other modules are evaluated.

The easiest solution I found to force load order is to simply require a module be loaded before continuing on with app initialization:

require(["bootstrapped-data-setup", "some-other-init-code"], function(){     require(["main-app-initializer"]); }); 
like image 161
tomconnors Avatar answered Sep 28 '22 02:09

tomconnors