js999
js999

Reputation: 2083

How to define a variable in require.config()

I have a file structure which looks like this:

/js
/vendor/
/spec
 |-main.js

spec/min.js is my entry point and from it I load modules in /js, /vendor, /spec

Actually to make the things working properly I need to put baseUrl: '../'.
Since the directory js, vendor and spec have many subdirectories, is quite boring to handle them, also because If I change something in file structure I need to change a lot of strings.

My question is: is possible using requirejs to set different paths or a variable to which refer?
Obviously, without defining any global variable.

Example:

require.config({
    baseUrl: '../',
    paths: {
       userView: 'js/users/views/userView' // how it works
       userView: baseDir + '/jquery' // possible solution where baseDir = js/users/views/
    }
});

Upvotes: 1

Views: 2433

Answers (1)

Simon Smith
Simon Smith

Reputation: 8044

To avoid creating global variables you could just wrap it in a self-invoking function:

(function() {
    var baseDir = 'something/';

    require.config({
        baseUrl: '../',
        paths: {
            userView: 'js/users/views/userView' // how it works
            userView: baseDir + '/jquery' // possible solution where baseDir = js/users/views/
        }
    });    
})();

Upvotes: 2

Related Questions