Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding requirejs paths

Using requirejs my main.js looks like this

requirejs.config({
    baseUrl: '/javascript/',
    paths: {
        jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min',
        async: 'requirePlugins/async',
            hbs: 'hbs'
    },
    waitSeconds: 7
});
define(['common'], function () {
    loadFonts();
});

The main.js is included in the page with a script call

<script data-main="/javascript/main.js" src="/javascript/require-2.0.1.js"></script>

Common is the basic function for the website, jquery doc ready function etc. wrapped in a define call:

define(['jquery'], function() {
    //jQuery dependant common code
});

This works fine, jQuery is loaded from the google CDN and the code is executed. But when i add a require call after the load of main.js

<script data-main="/javascript/main.js" src="/javascript/require-2.0.1.js"></script>
require(['jquery'], function ($) {
    //code
});

jquery is requested from /javascript/jquery.js instead of the defined path to the google cdn. I'm still a rookie at requirejs but it would seem to me that the path should be defined before any of the other requests are fired, can somebody please help me understand what I'm doing wrong?

like image 245
Hans Skov Avatar asked Jun 05 '12 09:06

Hans Skov


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.

What is the use of RequireJS?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.

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.

How do I use require config?

RequireJS can be initialized by passing the main configuration in the HTML template through the data-main attribute. It is used by RequireJS to know which module to load in your application. To include the Require. js file, you need to add the script tag in the html file.


2 Answers

I think this could be due to using the data-main attribute on the RequireJS script tag; for that to be parsed, RequireJS itself has to load and parse. In my testing (specifically for IE9), the browser would download and execute any script tags directly following the RequireJS script tag before parsing the RequireJS config file (the one specified by the data-main attribute).

To get around this, I simply quit using the data-main attribute and instead placed the config file as a normal script tag directly after the RequireJS script tag, and everything seems to be happy now.

Specifically, this is what it looks like (using your sample):

<script src="/javascript/require-2.0.1.js"></script>
<script src="/javascript/main.js"></script>
like image 63
ken Avatar answered Oct 07 '22 01:10

ken


Maybe you put the config statement before require js being loaded.

You should load require.js first, put your config code after, then call require(['jquery'], ...);

The reason it searches /javascript/ is because your require.js file is located there and it is the default base url.

Your config may never be used by require.js.

See this tutorial about require config.

like image 36
Chris Li Avatar answered Oct 07 '22 01:10

Chris Li