Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS works inconsistently

When I press F5 to reload my app sometimes throws errors and sometimes it does not.

I am debugging with Chrome. Sometimes the console reports this error:

Uncaught ReferenceError: unit_directionals is not defined

sometimes throws that a reference is not defined like in this case for jquery: "Uncaught ReferenceError: jQuery is not define"

What can be wrong if i have defined the files in the correct way?

this is the code I have in the main.js pointed in the main index html:

requirejs.config({
    baseUrl: 'js/lib',
    paths:{
        app:'../app',
        models: '../app/models',
        views: '../app/views'
    }
})

requirejs(
    [
        //load lib in this order
        'underscore', 'handlebars', 'jquery','backbone', 'uri',
        //load models, views...
        'app/models/items.model', 'app/models/results.model',
        'app/views/items.view', 'app/views/results.view',
        'app/index'
    ],
    function(jQuery,$,_....) {
        //init app
    }
);
like image 821
Ivan Juarez Avatar asked Sep 07 '12 18:09

Ivan Juarez


People also ask

Why do we need RequireJS?

RequireJS is a basic loader, which is used to loads the JavaScript files, it is a framework to manage dependencies between JavaScript files, and in modular programming, all the functionality divides in different modules, so RequireJs is a best tool to assemble different JavaScript files from different modules by which ...

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 RequireJS config?

It is used by RequireJS to know which module to load in your application. For instance − <script data-main = "scripts/main" src = "scripts/require.js"></script> To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module.


1 Answers

requirejs loads modules async and they can load out of order -- they are not guaranteed to load in the order specified in the require call. If the script is an AMD module, and calls define() with its dependencies, this is not a problem.

However, if the script just uses browser globals and implicit dependencies, like backbone and probably handlebars, then the shim config is needed to properly express the dependencies and export value.

like image 192
jrburke Avatar answered Sep 28 '22 22:09

jrburke