1

我无法使用 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()
    }
}

测试模型.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 包装器”,但我似乎也无法弄清楚它们是如何使用的。

4

2 回答 2

2

Mustache 确实将自己定义为 AMD 模块,因此不需要填充。我不知道 TypeScript 以及它如何与 RequireJS 集成,但请尝试从配置的 shim 数组中删除 Mustache。

于 2013-01-13T02:07:19.173 回答
1

我不太了解 TypeScript,但文档中提到编译器选项会将生成的 javascript 从 CommonJS 更改为 AMD。( --module amd)

Visual Studio TypeScript 选项

希望这可以帮助

于 2013-01-10T03:05:53.977 回答