简体   繁体   中英

Storing and loading variables in different Require.js modules

Im working on a backbone.js and require.js app. I have an app-constants.js file. It stores a number of static variable but also stores country iso codes:

define(function (require) {
    'use strict';


    var AppConstants = {
        SESSION_TIMEOUT_DURATION: 300,

        COUNTRY_ISO_CODES: [],

        getCountryIsoCodes: function () {
            return this.COUNTRY_ISO_CODES;
        },

        setCountryIsoCodes: function (countries) {
            this.COUNTRY_ISO_CODES = countries;
        }
    };
    return AppConstants;
});

The country iso codes is dependent on the language of the browser so it's loaded after the app starts. So after the language of browser is got, I then load country codes with:

appConstants.setCountryIsoCodes(lookUpDataCountries);

If i call getCountryIsoCodes() straight after setCountryIsoCodes(), I see the country codes are set in the appConstants file:

console.log('appConstants.getCountryIsoCodes() are ');
console.log(appConstants.getCountryIsoCodes());

Output is:

appConstants.getCountryIsoCodes() are
["CA", "MX", "GT",
etc etc

However, if I load the app-constants in another module, eg some model, and print out country codes again, I get an empty array. How do I store the country codes in app-constants.js file and load them into different modules?

Not sure if this will solve it, but how about:

define(function (require) {
    'use strict';

    var COUNTRY_ISO_CODES: [];

    var AppConstants = {
        SESSION_TIMEOUT_DURATION: 300,

        getCountryIsoCodes: function () {
            return COUNTRY_ISO_CODES;
        },

        setCountryIsoCodes: function (countries) {
            COUNTRY_ISO_CODES = countries;
        }
    };
    return AppConstants;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM