我在使用requirejs重建我的应用时出现问题。
上一个代码:
function getData(){
chrome.storage.local.get('settings',function(object){
var stored_data = object;
settings = stored_data['settings'];
//do something with settings object
createFromSettings(settings);
});
}
getData();
使用Requirejs(2个模块):
config.js:
requirejs.config({
baseUrl: '../src/js/modules',
paths: {
settings: 'getSettings',
create_elements: 'createElements'
}
getSettings.js
define(function(){
function localSettings(callback){
chrome.storage.local.get('settings',function(object){//async
var stored_data = object;
settings = stored_data['settings'];
callback(settings)
})
}
return{
getSettings:localSettings(function(data){
console.log(data)
//data is ok here
})
}
})
和createElements.js
define(['settings'],function(settings){
console.log(settings.getSettings)
//returns undefined
})
但它不起作用,因为我无法在异步中使用return。我无法理解如何在createElements.js模块中使用结果(设置)。
谢谢!