0

I am using backbone as a framework to display a list of all the projects. while rendering the underscore template im running into an 'Underscore.js' error says: SyntaxError: Unexpected token ILLEGAL

Following is my Backbone View:

define([
   'jquery',
    'underscore',
    'backbone',
    'collections/projects',
     ], function($, _, Backbone, ProjectsCollection){
         var ProjectListView = Backbone.View.extend({
              el: $(".content"),
              render: function(){
                 var self = this;
                  var projectlist = new ProjectsCollection();
                   projectlist.fetch({
                       success: function(projectlist){
                       var template = _.template($('#userList').html(), {projectlist:projectlist.models})
                       self.$el.html(template)
                      }
                 })
             }         
          });
       return ProjectListView;
 });

And below is my underscore template that im using to render the details:

Underscore:

<script type = "text/template" id = "userList">
    <table>
    <thead>
    <tr>
     <th>name</th>
      <th>title</th>
       <th>background</th>
    </tr>
    </thead>
    <tbody>
     <% _.each(projectlist, function(user){ %>
        <tr>
            <td><%= user.get('name') %></td>
            <td><%= user.get('title') %></td>
            <td><%= user.get('background') %></td>
        </tr>

     <% }) %>
    </tbody>
    </table>
    </script>

HTML to as a container to display the content in:

<div id="container">
<h1>User list</h1>
    <div id="menu">Welcome to the project</div>
        <div class="content"></div>
</div>

I tried debugging to see where the error might be coming from, at var template = _.template($('#userList').html(), {projectlist:projectlist.models}) step it breaks and runs into the ilegal token error in underscore.js file. I'm wondering the what might the error be. Please any ideass????

user2942566
  • 455
  • 4
  • 10
  • 22
  • I looked at it in jsbin and there it seems that there is an invisible character just before your closing `<% }) %>`. Try removing that whitespace line and see if it works. – Henrik Andersson May 25 '14 at 07:33
  • http://stackoverflow.com/questions/12719859/syntaxerror-unexpected-token-illegal – Anonymoose May 25 '14 at 11:42
  • I tried removing the line whitespace line between the '' and '<% }) %>'. I'm curious to know how you tried on the jsbin. can you send the link? Thanks – user2942566 May 25 '14 at 19:38
  • Could you delete all the lines with `user.get()` and add them back one by one, to check which specific attribute is causing the problem? Also, I may be mistaken, but shouldn't `self.$el.html(template)` be `self.$el.html(template())`? – Sharadh May 26 '14 at 07:09
  • oooohhh, well yes, i tried one by one and it magically worked..not sure what was worng..my guess some whitespaces might have caused the error..Thanks for your suggestions. – user2942566 May 28 '14 at 00:46

0 Answers0