我一直在盯着这一段时间,似乎无法弄清楚为什么一些AMD在加载它们作为依赖关系后不可用。我有一个称为“模型”的自定义模块;在我的MVC项目中配置了一个包含虚拟路径“/scripts/models.js”的包。当我在require.config中定义它并作为依赖项时,它将加载该文件。我可以看到它被请求和发现。从要求没有错误。但是,当我试图将它作为传入我的路由器的加载的依赖性参数引用时,它是未定义的(models.userModel)。需要js不加载模块
我在这里做错了吗?我没有看到任何循环依赖关系,我试图通过给它命名来定义模型模块。无论我是全局定义还是在我的router.js文件中通过路径请求模块,它都是未定义的。
app.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");
});
user.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引用时它是未定义的。在“联系”功能。
你不显示'models.js'代码 - 它返回/输出正确的值? – jevakallio
models.js是一个配置为从目录中提取全部内容的包。这个问题是我没有从models.js返回值(正如Andreas所述)。感谢您的期待! – Sullify