Search code examples
javascriptbackbone.jsunderscore.jsunderscore.js-templating

Fetching data from collection on backbone template


HI i am writing a simple Backbone program. i have written simple code to fetch data from collection on to the backbone template. But i am getting error Uncaught SyntaxError: Unexpected token <

here is my collection code:

var Album = Backbone.Collection.extend({

        url : "/data.json"
      });

this is my view code

var UserList= Backbone.View.extend({
el:'.page',
template:_.template($('#user-list-template').html()),
render : function(){
 var that=this;
 var album= new Album();
 album.fetch({
  success:function(album){
   alert("data fetched from collection");
   var _data = {data : album.models} ;
   $(that.el).html(that.template(_data));
  }
 })
}
});

and i am fetching data here:

<script type="text/javascript" id="user-list-template">
       <h1> <%= data.key %> </h1>

  </script>

Solution

  • Your template should be something like

    <script type="text/template" id="user-list-template">
    

    It cannot be of type javascript, because it isn't javascript... Hence the syntax error.