pryabov
pryabov

Reputation: 854

Default RequireJS Context Config

I have two modules:

module1.js:

define([], function () {
    var self = {};

    self.name = function () {
        return 'module1';
    };

    return self;
});

module2.js:

define([], function () {
    var self = {};

    self.name = function () {
        return 'module2';
    };

    return self;
});

And index.html page is:

<html>
    <head>
        <script src="http://requirejs.org/docs/release/2.1.10/minified/require.js"></script>
        <script type="text/javascript">
        var require1 = require.config({
            context: 'context1',
            baseUrl: '.',
            paths: {
                'mod': 'module1'
            }
        });

        var require2 = require.config({
            context: 'context2',
            baseUrl: '.',
            paths: {
                'mod': 'module2'
            }
        });

        require1(['mod'], function (module) {
            console.log('require1 : ' + module.name());
        });

        require2(['mod'], function (module) {
            console.log('require2 : ' + module.name());
        });

        require(['mod'], function (module) {
            console.log('require : ' + module.name());
        });
        </script>
    </head>
    <body>
    </body>
</html>

How can I specify which config will be used by default for last call:

    require(['mod'], function (module) {
        console.log('require : ' + module.name());
    });

Also if you know some intersting article about requireJS context it will be great if you share it.

Upvotes: 1

Views: 1366

Answers (1)

Simon Boudrias
Simon Boudrias

Reputation: 44589

The last call will use the default/global context. In your case, you haven't setup any config for this context, so it'll use default values.

To add a config to the default context, simply do so using require.config() without specifying a context name.

Upvotes: 2

Related Questions