Precompiled Handlebars template with RequireJS

1k views Asked by At

I have a handlebars template which I have manually precompiled and saved as - testTemplate.handlebars.

Now, in my requireJS + Backbone code I have the below function -

define(['text!../templates/testTemplate.handlebars'
       ],function(testTemplate){

           var myView = Backbone.View.extend(

              initialize: function(options){

                  this.template = Handlebars.template(testTemplate);

              },

              render: function(data){

                  $(this.el).html(this.template(data));
              }

           );
});

So the testTemplate.handlebars returns the Javascript code in a form of string which when passed to Handlebars.template returns the JS function. When I tried to print on the console the value I get in the this.template variable it shows -

function (n,r){return r=r||{},e.call(t,Handlebars,n,r.helpers,r.partials,r.data)}

But, when the line - $(this.el).html(this.template(data)); of the render function executes, it gives an error message saying - Uncaught Typeerror : object has no method call. (Even though I can see a e.call function)

Am I doing something wrong here ?

Also, when I try to compile the template runtime the render function works. While runtime compilation Handlebars.compile(testTemplate) the below function is returned -

function (e,t){return n||(n=r()),n.call(this,e,t)}
1

There are 1 answers

0
David Bashford On

If you have precompiled it, I'm not sure you need to make the .template call. The function you are given should be executable AS the template itself. So this:

$(this.el).html(this.template(data));

becomes this:

$(this.el).html(testTemplate(data));