HandleBars与RequireJS和BackboneJS编译

时间:2013-08-20 03:32:32

标签: backbone.js requirejs handlebars.js

requirejs配置:

require.config({
    baseUrl: '/js/',
    paths: {
        jquery: './libs/jquery/jquery-1.10.1.min',
        underscore: './libs/underscore/underscore-min',
        backbone: './libs/backbone/backbone-min',
        handlebars: './libs/handlebars/handlebars',
        templates: '/templates'
    },

    shim: {
        underscore: {
            exports: '_'
        },

        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        }
    }
});

查看:

define([
  'backbone',
  'handlebars',
  'text!templates/mytemplate.html'
], function(Backbone, Handlebars, Template){

    MyView = Backbone.View.extend({
        tagName: 'li',
        template: Handlebars.compile(Template),

        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });

    return MyView;
});

我遇到以下错误:

Uncaught TypeError: Cannot call method 'compile' of undefined.

1 个答案:

答案 0 :(得分:4)

将此添加到您的垫片配置:

shim: {
  handlebars: {
    exports: 'Handlebars'
  },