在javascript库中使用JSON配置文件

在javascript库中使用JSON配置文件,javascript,almond,Javascript,Almond,我使用almond.js来实现我的javascript库。我试图从代码中分离配置变量。我创建了config.json文件,其中包含所有“全局”变量,并定义了“模型”,将注入其他模块: define("Model", [], function () { "use strict"; var model = { options: { } }; (function () { var xhr = new XMLHttpRe

我使用almond.js来实现我的javascript库。我试图从代码中分离配置变量。我创建了config.json文件,其中包含所有“全局”变量,并定义了“模型”,将注入其他模块:

define("Model", [], function () {
    "use strict";

    var model = {
        options: {
        }
    };

    (function () {
        var xhr = new XMLHttpRequest();
        xhr.overrideMimeType("application/json");
        xhr.open('GET', 'Config/config.json', true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 300)) {
                model.options = JSON.parse(xhr.response);
            }
        };
        xhr.send();
    })();

    return model;
});
define(“模型”、“函数”){
“严格使用”;
var模型={
选项:{
}
};
(功能(){
var xhr=new XMLHttpRequest();
重写emimetype(“application/json”);
open('GET','Config/Config.json',true);
xhr.onreadystatechange=函数(){
如果(xhr.readyState==4&&(xhr.status>=200&&xhr.status<300)){
model.options=JSON.parse(xhr.response);
}
};
xhr.send();
})();
收益模型;
});
问题是,然后我使用我的“模型”定义了多个模块,请求仍然没有完成,所有的“选项”参数仍然
未定义
。 是否有任何方法可以实现将等待模型初始化的依赖项,或者有其他方法可以通过almond.js实现此功能

我是否应该用require.js替换almond.js以实现此功能?如果可以,会是什么样子


提前感谢。

我找到了问题的解决方案: 更改之前,我的启动程序脚本是:

(function () {
    "use strict";
    require("Core");
})();
其中“核心”是包含所有依赖项的主模块

变化: 我删除了static
define(“Model”,[],function(){…})

我的发射器变成了:

(function () {
    "use strict";
    (function () {
        var xhr = new XMLHttpRequest();
        xhr.overrideMimeType("application/json");
        xhr.open('GET', 'Config/config.json', true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 300)) {
                var model = {
                    options: JSON.parse(xhr.response)
                };

                if (define && define.amd) {
                    define("Model", model);
                    require("Core"); 
                }
            }
        };
        xhr.send();
    })();
})();
(函数(){
“严格使用”;
(功能(){
var xhr=new XMLHttpRequest();
重写emimetype(“application/json”);
open('GET','Config/Config.json',true);
xhr.onreadystatechange=函数(){
如果(xhr.readyState==4&&(xhr.status>=200&&xhr.status<300)){
var模型={
选项:JSON.parse(xhr.response)
};
if(define&&define.amd){
定义(“模型”,模型);
要求(“核心”);
}
}
};
xhr.send();
})();
})();

现在,所有模型选项都从config.json文件中解析出来,并在我运行所有依赖项链时准备就绪。

我找到了问题的解决方案: 更改之前,我的启动程序脚本是:

(function () {
    "use strict";
    require("Core");
})();
其中“核心”是包含所有依赖项的主模块

变化: 我删除了static
define(“Model”,[],function(){…})

我的发射器变成了:

(function () {
    "use strict";
    (function () {
        var xhr = new XMLHttpRequest();
        xhr.overrideMimeType("application/json");
        xhr.open('GET', 'Config/config.json', true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 300)) {
                var model = {
                    options: JSON.parse(xhr.response)
                };

                if (define && define.amd) {
                    define("Model", model);
                    require("Core"); 
                }
            }
        };
        xhr.send();
    })();
})();
(函数(){
“严格使用”;
(功能(){
var xhr=new XMLHttpRequest();
重写emimetype(“application/json”);
open('GET','Config/Config.json',true);
xhr.onreadystatechange=函数(){
如果(xhr.readyState==4&&(xhr.status>=200&&xhr.status<300)){
var模型={
选项:JSON.parse(xhr.response)
};
if(define&&define.amd){
定义(“模型”,模型);
要求(“核心”);
}
}
};
xhr.send();
})();
})();

现在所有的模型选项都是从config.json文件中解析出来的,在我运行所有依赖项链时就准备好了。

Almond根本不支持动态加载,所以我怀疑这是可能的,甚至是有意义的。我可以用require.js替换ammond.js。它将帮助我实现我所需要的?Almond根本不支持动态加载,所以我怀疑这是可能的,甚至是有意义的Hi Patrick。我可以用require.js替换ammond.js。它将帮助我实现我所需要的?