我在CoffeeScript中有以下(非常简单的)模块定义:
# backbone/routers/appointments_router.js.coffee
define ["app", "underscore", "backbone"], (App, _, Backbone) ->
console.log(Backbone)
这是我的配置和内容:
# application.js.coffee
requirejs.config
paths:
underscore: "lodash.min"
backbone: "backbone"
appointmentsRouter: "backbone/routers/appointments_router"
"backbone-relational": "backbone-relational"
requirejs ["app", "underscore", "backbone", "appointmentsRouter"], (App, _, Backbone, AppointmentsRouter) ->
以下是正在发生的事情:当我加载页面时,我在控制台中获得undefined
,即使Backbone被列为依赖项。更令人费解的是,如果我在控制台中键入Backbone
,则定义了Backbone 。
Backbone最终会如何评估,但我的appointments_router.js.coffee
不了解Backbone?
答案 0 :(得分:1)
Underscore或Backbone不符合AMD标准,因此定义路径是不够的。幸运的是,Require.js提供了shim
-functionality作为答案。
所以你必须添加这样的东西
requirejs.config( // shouldn't this be just require?
paths: ..., // don't change these
shim: {
"underscore": {
exports: "_" // define the export
},
"backbone": {
deps: ["underscore"], // define dependencies for backbone
exports: "Backbone"
}
}
);
希望这有帮助!