我已经盯着这个看了一段时间,似乎无法弄清楚为什么某些 AMD 在将它们作为依赖项加载后不可用。我有一个名为“模型”的自定义模块;在我的 MVC 项目中使用虚拟路径“/scripts/models.js”配置的包。当我在 require.config 中定义它并作为依赖项时,它会加载文件。我可以看到它被请求并找到了。要求没有错误。但是,当我尝试将它作为传递给我的路由器的加载依赖项参数引用时,它是未定义的(models.userModel)。
我在这里做错了什么吗?我没有看到任何循环依赖项,我尝试通过给模型模块命名来定义它。无论我是全局定义它还是通过 router.js 文件中的路径请求模块,它都是未定义的。
应用程序.js。主要配置。(以下)
require.config({
baseUrl: "/scripts/app",
paths: {
jquery: "../jquery",
underscore: "libs/underscore",
backbone: "libs/backbone",
kendo: "libs/kendo",
models: "../models"
},
// We shim Backbone since it doesn't declare an AMD module
shim: {
underscore: {
exports: "_"
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
}
},
});
require([
"jquery",
"backbone",
"kendo",
"models",
"router"
], function ($, backbone, kendo, models, router) {
alert("config-start");
});
用户.js。包含在 models.js 包中。(以下)
define({
userModel : kendo.observable({
datasource: kendo.data.DataSource({
transport: {
read: {
url: "/api/usersettings",
dataType: "json",
type: "GET"
},
update: {
url: "/api/usersettings",
dataType: "json",
type: "PUT"
}
},
schema: {
model: {
id: "UserId"
}
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return {
models: kendo.stringify(options.models)
};
}
return options;
}
}),
save: function () {
this.data.sync();
},
})
});
router.js 文件(下)
define(["jquery",
"backbone",
"models"
], function ($, backbone, models) {
/**
* The Router class contains all the routes within the application -
* i.e. URLs and the actions that will be taken as a result.
*
* @type {Router}
*/
var Router = Backbone.Router.extend({
contentArea: $("#MainContent"),
routes: {
"user/usersettings/contact": "contact",
"user/usersettings/security": "security",
"user/usersettings/dashboard": "dashboard",
"user/usersettings/permissions": "permissions",
"user/usersettings/colors": "colors"
},
contact: function () {
var contactTemplate = kendo.template($("#usersettings-usercontact").html());
this.contentArea.empty();
this.contentArea.html(contactTemplate);
kendo.bind(this.contentArea, models.userModel); // models is undefined
},
security: function () {
},
dashboard: function () {
},
permissions: function () {
},
colors: function () {
}
});
// Create a router instance
var router = new Router();
//Begin routing
Backbone.history.start();
return router;
});
也许我遗漏了一些明显的东西,但我无法将“模型”加载为外部依赖项。从 router.js 引用时未定义。关于“联系”功能。