使用requirejs加载小胡子

使用requirejs加载小胡子

问题描述:

我想通过requirejs加载和使用小胡子。

I would like to load and use mustache by requirejs.

也许这个问题已经问过:

使用RequireJS使用Mustache的模块加载错误

Maybe this question has already asked:
AMD Module Loading Error with Mustache using RequireJS

无论如何,我想弄清楚如何修复我的代码:

Anyway I am trying to figure out how can I fix my code:

main.js

require.config({
    paths: {
        jquery: 'libs/jquery/jquery',
        underscore: 'libs/underscore/underscore-min',
        backbone: 'libs/backbone/backbone-optamd3-min',
        mustache: "libs/mustache/mustache"
    }
});

require([
    'views/app'
    ], function(AppView){
        var app_view = new AppView;
 });






app.js

define([
    'jquery',
    'underscore', 
    'backbone',
    "mustache"
    ], function($, _, Backbone, Mustache) {
        console.log($, _, Backbone, Mustache); // <-- *** Mustache is null ***
        // ......
       }
);






正如您在 app.js 文件, Mustache为空 ...

我应该使用另一个Mustache库吗?
这里我使用的是 Moustache

您应该在胡子目录中创建一个新文件mustache-wrap.js,如下所示:

You should just create in your mustache directory a new file mustache-wrap.js which looks like this:

 define(['libs/mustache/mustache'], function(Mustache){
    // Tell Require.js that this module returns a reference to Mustache
    return Mustache;
 });

然后你的主要是:

  mustache: "libs/mustache/mustache-wrap"