[英]Storing and loading variables in different Require.js modules
Im working on a backbone.js and require.js app. 我正在工作的是ribs.js和require.js应用程序。 I have an app-constants.js file.
我有一个app-constants.js文件。 It stores a number of static variable but also stores country iso codes:
它存储许多静态变量,但也存储国家/地区iso代码:
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: 如果我在setCountryIsoCodes()之后直接调用getCountryIsoCodes(),我会看到在appConstants文件中设置了国家代码:
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?
如何将国家/地区代码存储在app-constants.js文件中,并将其加载到其他模块中?
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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.