Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple path specifications in RequireJS

I want to be able to inject extra paths, in a file different from the one which contains the config. Can this be done? A bonus question is whether I can directly access "config" variables.

like image 586
sabof Avatar asked Sep 30 '13 08:09

sabof


People also ask

What is Shim RequireJS?

shim: 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. Here is an example. It requires RequireJS 2.1. 0+, and assumes backbone. js, underscore.

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

RequireJs is basically a JavaScript for the specific module. RequireJs use AMD Standards. RequireJs don't allow to run global JavaScript. If you have to use JavaScript then add into RequireJS configuration file. RequireJs share code and data between modules and programs.


1 Answers

There is no problem with calling require.config multiple times or from multiple places. You don't have to provide an entire set of configuration on subsequent calls. The new path mappings will be merged with existing ones.

For example, if you did this originally:

require.config({
    paths: {
        foomodule: 'libs/foo',
        jquery:  'libs/jquery'
    }
});

You could later do this to provide a different set of paths for jquery and/or to inject paths for a whole new module not present in the original config:

require.config({
    paths: {
        jquery:  [ 'http://code.jquery.com/jquery-2.0.2', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2' ],
        // note that foomodule not provided here but still keeps its original configuration
        someothermodule: 'some/other/path'
    }
});

Note, however, that if a module was already loaded based on the original config and you wanted to force it to reload from the new config you might have to call require.undef

Regarding the 2nd part of your question (reading the existing config information), I asked a question on this too and so far have not found a way to do it.

like image 70
explunit Avatar answered Oct 10 '22 02:10

explunit