voirmg
Last Updated: February 25, 2016
·
1.437K
· smallhadroncollider
Fb0153b7c04243e7fe2d1ec64f5daa91

Using a config file with RequireJS

It can be very useful to have a single configuration file that can be used anywhere in your JavaScript application. This is really easy to do with RequireJS.

First setup your config.js file and place it in your root JS directory:

define({
    environment: 'development',

    // API settings
    api: {
        url: 'api.someurl.com',
        alwaysUseHTTPS: true,
        clientID: 'xxxxxxxxxxxxxx'
    }
});

Then you can use it anywhere in your code:

define(['config'], function (config) {
    // Setup logging
    if (config.environment === 'development') {
        console.log('Running in Development Mode');
    }

    // App code...
});
Say Thanks
Respond