JR Galia
JR Galia

Reputation: 17269

HandleBars Compile w/ RequireJS and BackboneJS

requirejs configuration:

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'
        }
    }
});

View:

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;
});

I encountered the following error:

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

Upvotes: 2

Views: 2862

Answers (1)

ekeren
ekeren

Reputation: 3458

add this to your shim configuration:

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

Upvotes: 4

Related Questions