【发布时间】:2013-01-13 10:07:19
【问题描述】:
我在使用 RequireJS 将 mustache.js 作为 AMD 模块加载时遇到问题(用于在 Backbone 模型中使用)。我也开始使用 TypeScript,因此没有完全遵循依赖和声明的流程!
从 app.ts 开始:
require.config({
paths: {'jquery': 'lib/jquery-1.8.3.min', 'underscore': 'lib/underscore.min', 'backbone': 'lib/backbone.min', 'mustache': 'lib/mustache', 'appmain': 'AppMain'},
shim: {mustache: { deps: ["jquery"] }, backbone: { deps: ["underscore", "jquery"] }, appmain: {deps: ["backbone", "mustache"] } } });
require(['appmain'], (main) => {
var appMain = new main.AppMain();
appMain.run();
});
appmain.ts:
import tm = module("models/TestModel");
export class AppMain {
public run() {
var testModel = new tm.TestModel()
}
}
TestModel.ts:
/// <reference path="../modules/backbone.d.ts"/>
export class TestModel extends Backbone.Model {
constructor(options?) {
super(options);
};
initialize() {
var stringTemplate = "{{something}}";
Mustache.compile(stringTemplate);
};
}
我收到一个 javascript 错误,提示“未定义 Mustache”,尽管 mustache.js 确实在 TestModel.js 之前加载。我只是从这里使用 mustache.js:https://github.com/janl/mustache.js,所以我想我可能不明白 AMD 模块是如何正确声明的。
我看到这个项目中有一些“AMD 包装器”,但我似乎也不知道它们是如何使用的。
【问题讨论】:
-
你的小胡子垫片需要导出一些东西
-
Mustache 也不依赖于 jQuery
标签: backbone.js requirejs typescript mustache