Backbone.js 不需要加载模块
我已经关注这一点有一段时间了,似乎不明白为什么一些AMD在作为依赖项加载后不可用。我有一个名为“模型”的自定义模块;在我的MVC项目中使用虚拟路径“/scripts/models.js”配置的捆绑包。当我在require.config中将其定义为依赖项时,它将加载该文件。我可以看到它是被请求和找到的。不需要任何错误。然而,当我试图引用它作为传入路由器的已加载依赖项参数时,它是未定义的(models.userModel) 我有什么地方做错了吗?我没有看到任何循环依赖项,我已经尝试通过给它命名来定义models模块。它是未定义的,无论我是全局定义它还是在router.js文件中通过路径请求模块 app.js。主配置。(下文) user.js。包含在models.js包中。(下文) router.js文件(如下)Backbone.js 不需要加载模块,backbone.js,requirejs,single-page-application,Backbone.js,Requirejs,Single Page Application,我已经关注这一点有一段时间了,似乎不明白为什么一些AMD在作为依赖项加载后不可用。我有一个名为“模型”的自定义模块;在我的MVC项目中使用虚拟路径“/scripts/models.js”配置的捆绑包。当我在require.config中将其定义为依赖项时,它将加载该文件。我可以看到它是被请求和找到的。不需要任何错误。然而,当我试图引用它作为传入路由器的已加载依赖项参数时,它是未定义的(models.userModel) 我有什么地方做错了吗?我没有看到任何循环依赖项,我已经尝试通过给它命名来定义
也许我遗漏了一些明显的东西,但我无法将“模型”作为外部依赖加载。从router.js引用时未定义。在“联系”功能上。定义需要一个返回值的函数,该值将在另一个模块中需要时注入
Define需要一个返回值的函数,该值将在另一个模块中需要时被注入
您没有显示
models.js
的代码-它是否正确返回/导出值?models.js是一个配置为从目录中提取所有内容的捆绑包。问题是我没有从models.js返回值(正如Andreas所说)。谢谢你的关注!您没有显示models.js
的代码-它是否正确返回/导出值?models.js是一个配置为从目录中提取所有内容的捆绑包。问题是我没有从models.js返回值(正如Andreas所说)。谢谢你的关注!是的,我明白了。就是这样。谢谢你抽出时间。谢谢!是的,我明白了。就是这样。谢谢你抽出时间。谢谢!
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");
});
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();
},
})
});
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;
});
/**
* The function that handles definitions of modules. Differs from
* require() in that a string for the module should be the first argument,
* and the function to execute after dependencies are loaded should
* return a value to define the module corresponding to the first argument's
* name.
*/
define(function(){
return {
userModel: ...
}
})