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

时间:2012-02-24 23:01:17

标签: backbone.js requirejs mustache

我在这里关注tuturoial:http://backbonetutorials.com/organizing-backbone-using-modules/我想做的就是在Backbone View中使用Mustache而不是underscore.js作为我的模板引擎。一切都按预期工作,直到我尝试更换Moustache。 Firebug给了我这个:

Load timeout for modules: Mustache

My Mustache包装器(在libs/mustache/mustache-wrap.js中)就像这样:

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

以下是我的Backbone View的代码:

// Protocol Detail View
define([
  'jQuery',
  'Underscore',
  'Backbone',
  'Mustache',
  'collections/protocols',
  'text!templates/protocol/protocoldetail.html'
], function($, _, Backbone, Mustache, protocolCollection, protocolDetailTemplate){

    var protocolDetailView = Backbone.View.extend({

        el: "#asset-detail",
        render: function( pid ){
            this.collection = new protocolCollection;
            this.collection.fetch();
            var p = this.collection.getByCid('c'+pid);
            var template = "{{name}}";
            htmlr = Mustache.to_html(template, p);
            $(this.el).html(htmlr);
            //var compiledTemplate = _.template( protocolDetailTemplate, { protocol: protocol });
            //$(this.el).html(compiledTemplate);

        },

        events: {
            "submit #asset-owner": "chown"
        },

        chown: function ( pid ){
            console.log("Protocol Detail View chown callback.")
        }
    });

    return new protocolDetailView;
});

我的main.js文件具有以下配置:

require.config({
    paths: {
      jQuery: 'libs/jquery/jquery',
      jQueryUI: '//ajax.aspnetcdn.com/ajax/jquery.ui/1.8.17/jquery-ui.min',
      jstree: 'libs/jstree/jquery.jstree',
      Mustache: 'libs/mustache/mustache-wrap',
      Underscore: 'libs/underscore/underscore',
      Backbone: 'libs/backbone/backbone'
  }
});

3 个答案:

答案 0 :(得分:2)

在你拥有所有需要的main.js文件中,确保将Mustache添加到配置中,不要担心包装器,只需尝试直接加载Mustache。

require.config({ 
    'paths': { 
        "underscore": "libs/underscore", 
        "backbone": "libs/backbone",
        "Mustache": "libs/mustache/mustache"
    }
}); 

这可能会有所帮助......

此外,这里有一些入门应用程序可以帮助您完成您的需求,骨干之旅。

https://github.com/jcreamer898/RequireJS-Backbone-Starter
https://github.com/david0178418/BackboneJS-AMD-Boilerplate
https://github.com/addyosmani/backbone-fundamentals
https://github.com/amdjs

答案 1 :(得分:2)

从github克隆存储库:

$ git clone https://github.com/janl/mustache.js.git

然后,构建库的RequireJS特定版本:

$ rake requirejs

使用生成的文件'requirejs.mustache.js'作为胡须库。

答案 2 :(得分:0)

使用Twitter's mustache repository

这对我有用(Ubuntu 12.04):

sudo apt-get install rake ruby-rspec
git clone git://github.com/twitter/mustache.js.git
cd mustache.js/
rake requirejs