Handling global varibales using AMD Requirejs + Backbonejs

As in the application, I need to have some configured variables that must be global, and I use them through modules. My configuration file may look like.

config.js

var config = { baseServiceUrl: 'http://localhost/baseServiceUrl', baseUrl: 'http://localhost/baseUrl', mapping_764: { location:'Austin', emailAddress:' austin@customerservice.com ', registerLink:'https://www.customercare.com/en/austin/registration/', fbLikeLink:'https://www.facebook.com/customercare.austin', microSite: 'http://austin.customercare.com/' } } 

I just upload this file with a script tag along with requirejs.
<script src="js/app/config.js"></script>
<script data-main="js/main" src="js/libs/require/require.js"></script>

Now the global variable (object) config can be used in all modules, as well as from the browser console. So I thought that if someone changes these configuration attributes, the application will certainly crash because this configuration variable is used for office calls and for many other things.

Is there any way to deal with such problems.?

+4
source share
1 answer

A few things can help you:

First, for pure site configuration information (such as a REST URL), RequireJS has a configuration API. From the documentation :

 requirejs.config({ config: { 'bar': { size: 'large' }, 'baz': { color: 'blue' } } }); //bar.js, which uses simplified CJS wrapping: //http://requirejs.org/docs/whyamd.html#sugar define(function (require, exports, module) { //Will be the value 'large' var size = module.config().size; }); 

There is also this question , which addresses the general case of passing global variables if you do not want to use the configuration API.

Secondly, for the initial loading of data in the Backbone's model, a broad issue is being discussed

+5
source

Source: https://habr.com/ru/post/1480867/


All Articles