我使用Requirejs
在我们的Web应用程序中加载JavaScript。问题是,我得到一个undefined
对象被传递给一个模块,当在其他模块中使用时,它被很好地实例化。未定义的对象通过Requirejs
好的,这是设置。我main.js
文件,该文件requirejs在启动时运行:
require.config({
baseUrl: "/scripts",
paths: {
demographics: "Demographics/demographics",
complaints: "Complaints/complaints",
}
});
require(["templates", "demographics", "complaints", "crossDomain"], function (templates, demographics, complaints) {
"use strict";
console.log("0");
console.log(demographics === undefined);
demographics.View.display();
});
很多的配置已经被剥夺,只是在这个问题上的核心文件。
这里的Demographics.js
:
define(["ko", "templates", "complaints", "globals", "underscore"], function (ko, templates, complaints, globals) {
// Stuff removed.
return {
View: view
};
});
和Complaints.js
define([
"demographics",
"ko",
"templates",
"complaints",
"visualeffects",
"globals",
"webservice",
"underscore",
"typewatcher",
"imagesloaded"],
function (demographics, ko, templates, complaints, visualeffects, globals, webservice) {
"use strict";
console.log("1");
console.log(demographics === undefined);
return {
View: view
};
});
问题是这样的 - 在Complaints.js
通过define
配置通过demographics
参数为undefined
。控制台注销告诉我“人口统计数字===未定义”是true
。
但是,当main.js文件执行时,传递给它的人口统计参数不是未定义的,而是如预期的那样是一个实例化对象。
现在我被卡住了,因为我看不到为什么在complaints.js
人口统计变量未定义。任何人都可以发现我错过的东西吗?
啊,正是我所怀疑的,但真的没有想到会发生这种情况(我是一个真正的新手,这AMD/requirejs的东西:)我的行动计划是创建另一个模块,将托管两个代码人口统计和抱怨。这意味着这个新模块将用于调用其他两个文件的代码。感谢您确认我的想法。当我得到时间时会试一试。 – 2012-08-16 19:46:46