Backbone.js template error with coffeescript and eco templating language

1.5k Views Asked by At

Solved:

I have a backbone(0.9.2) written in coffeescript as follows

class Animal.Views.Cats extends Backbone.View
 template: JST['animals/cats']
 ...
 ...
 render: ->
  $(@el).html(@template())
  this

With this coffeescript code, when i execute in browser, i get the following error in console

Property template of object <#cat> is not a function

I am using eco template with rails 3.1 backend where am i going wrong?

solution:

the problem was a deeply nested template file structure

template: JST['mammals/animals/cats'] fixes the problem

2

There are 2 best solutions below

2
On

in your code 'template' is not defined as a function but as a attribute.

try

template: -> JST['animals/cats']

ie, insert the function arrow '->'

or, if you do not want that to be a function, then drop the parentheses after @template

$(@el).html @template
0
On

To reiterate, if you have a directory structure like this:

app/assets/templates/namespace/animals/cats.jst.eco

You need to include your namespace when referencing your template:

  • Will not work: JST['animals/cats']
  • Will work: JST['namespace/animals/cats']