2

需要js配置:

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.
4

1 回答 1

4

将此添加到您的 shim 配置中:

shim: {
  handlebars: {
    exports: 'Handlebars'
  },
于 2013-08-20T06:30:08.750 回答