小胡子是空的

时间:2014-03-02 19:34:17

标签: javascript backbone.js requirejs mustache

所以我已经查看了这个条目:Loading mustache using requirejs

然而,它仍然没有解决我的问题。当我尝试使用Mustache编译html文件时,它说Mustache为null。

如果以下文件不足以确定问题,我的回购邮件是here

main.js

require.config({
    baseUrl: 'scripts/lib',
    paths:{
        util : '../util',
        models : '../util/models',
        views : '../util/views',
        collections : '../util/collections',
        routers : '../util/routers',
        templates : '../../templates',
        mustache : 'mustache/mustache',
        mustacheWrap: 'mustache/mustachewrapper'
    },
    shim:{
        'underscore' : {
            exports: '_'
        },
        'backbone' : {
            deps:['underscore', 'jquery'],
            exports: 'Backbone'
        }
    }

});

require([
    'util/init'
], 
function(App) {

    App.init();
    console.log("All files loaded successfully!");

});

DisplaySeries.js

define([
    'underscore',
    'jquery',
    'backbone',
    'mustacheWrap',
    'text!templates/Comic.html',
    'collections/Comics'
],
function(_, $, Backbone, Mustache, Template, ComicCollection) {

    return Backbone.View.extend({

        el: '#pane-container',

        compiledTemplate: Mustache.compile(Template), <------ Mustache is Null

        initialize:function() {
            console.log("DisplaySeriesView: created...");
            this.initData();
        },

        initData:function() {
            this.collections.comics = new ComicCollection();
            this.collections.comics.fetch({
                success:function() {
                    console.log("DisplaySeriesView: fetch succeeded.");
                    this.JSON.comics = this.collections.comics.toJSON();
                    this.render();
                },
                error:function() {
                    console.error("DisplaySeriesView: fetch failed.");
                }
            });
        },

        render:function() {
            this.$el.html(compiledTemplate({comics : this.JSON.comics}));
            return this;
        }
    });
});

1 个答案:

答案 0 :(得分:0)

首先,看起来你在下载Mustache.js时抓住了错误的文件。 https://github.com/HerrPfister/comic-checklist-app/blob/master/app/scripts/lib/mustache/mustache.js是Mustache.js文件的实际HTML页面(即非原始源代码)。

此外,您链接的问题已过时(我甚至可以看到flagged that a year ago)。 Mustache已经符合AMD标准,所以你应该能够通过'mustache'简单地要求它,只要它包含在paths配置元素中:

requirejs.config({
  paths: {
    mustache: 'path/to/mustache'
  }
});

require([
  'mustache'
], function (Mustache) {
  console.log('Mustache loaded?', Mustache);
});