2013-10-21 25 views
0

我下面這個教程 - https://www.youtube.com/watch?v=FZSjvWtUxYkBackbone.js的 - ncaught例外:語法錯誤:函數的構造:無法編譯功能

我被困在這個部分(我開始收到錯誤之後的部分視頻開始): https://www.youtube.com/watch?feature=player_detailpage&v=FZSjvWtUxYk#t=1678

正如我在錯誤中看到它與underscore.js模板有關。

Uncaught exception: SyntaxError: Function constructor: failed to compile function 

Error thrown at line 637, column 3 in <anonymous function: T.template>(e, t, n) in http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js: 
    throw u.source = s, u 
called from line 57, column 12 in <anonymous function: success>() in file://localhost/home/andrius/dev/backbone/beginner/index.html: 
    var template = _.template($('#user-list-template').html(), {users: users.models}) ; 
called from line 442, column 4 in <anonymous function: a.success>(d, e, f) in http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js: 
    c && c(b, d) 
called via Function.prototype.apply() from line 870, column 5 in <anonymous function: p.Callbacks>(b) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    if (i[h].apply(b[0], b[1]) === !1 && a.stopOnFalse) 
called from line 915, column 5 in <anonymous function: fireWith>(a, b) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    return b = b || [], b = [a, b.slice ? b.slice() : b], i && (!d || j) && (e ? j.push(b) : k(b)), this 
called from line 3650, column 4 in y(a, c, f, i) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    x.status = a, x.statusText = (c || y) + "", k ? o.resolveWith(m, [s, y, x]) : o.rejectWith(m, [x, y, t]), x.statusCode(r), r = b, j && n.trigger("ajax" + (k ? "Success" : "Error"), [x, l, k ? s : t]), q.fireWith(m, [x, y]), j && (n.trigger("ajaxComplete", [x, l]), --p.active || p.event.trigger("ajaxStop")) 
called from line 3879, column 6 in <anonymous function: d>(a, e) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    l && f(h, j, l, k) 

Error initially occurred at line 634, column 3 in <anonymous function: T.template>(e, t, n) in http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js: 
    var o = new Function(n.variable || "obj", "_", s) 
called from line 57, column 12 in <anonymous function: success>() in file://localhost/home/andrius/dev/backbone/beginner/index.html: 
    var template = _.template($('#user-list-template').html(), {users: users.models}) ; 
called from line 442, column 4 in <anonymous function: a.success>(d, e, f) in http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js: 
    c && c(b, d) 
called via Function.prototype.apply() from line 870, column 5 in <anonymous function: p.Callbacks>(b) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    if (i[h].apply(b[0], b[1]) === !1 && a.stopOnFalse) 
called from line 915, column 5 in <anonymous function: fireWith>(a, b) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    return b = b || [], b = [a, b.slice ? b.slice() : b], i && (!d || j) && (e ? j.push(b) : k(b)), this 
called from line 3650, column 4 in y(a, c, f, i) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    x.status = a, x.statusText = (c || y) + "", k ? o.resolveWith(m, [s, y, x]) : o.rejectWith(m, [x, y, t]), x.statusCode(r), r = b, j && n.trigger("ajax" + (k ? "Success" : "Error"), [x, l, k ? s : t]), q.fireWith(m, [x, y]), j && (n.trigger("ajaxComplete", [x, l]), --p.active || p.event.trigger("ajaxStop")) 
called from line 3879, column 6 in <anonymous function: d>(a, e) in http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js: 
    l && f(h, j, l, k) 

我的代碼(不看在我的代碼和視頻有什麼區別..)

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>BackboneTutorials.com Beginner Video</title> 
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css"> 
</head> 
<body> 
    <div class="container"> 
    <h1>User Manager</h1> 
    <hr /> 
    <div class="page"></div> 
    </div> 

    <script type="text/template" id="user-list-template"> 
    <table class="table striped"> 
     <thead> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Age</th> 
      <th></th> 
     </tr> 
     </thead> 
     <tbody> 
     <% _.each(users, function(user) { %> 
      <tr> 
      <td><%= user.get('firstname') %></td> 
      <td><%= user.get('lastname') %></td> 
      <td><%= user.get('age') %></td> 
      <td></td> 
      </tr> 
     <%)}; %> 
     </tbody> 
    </table> 
    </script> 

    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script> 
    <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js" type="text/javascript"></script> 
    <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script> 
    <script> 

    $.ajaxPrefilter(function(options, originalOptions, jqXHR) { 
     options.url = 'http://backbonejs-beginner.herokuapp.com' + options.url; 
    }); 

    var Users = Backbone.Collection.extend({ 
     url: '/users' 
    }); 

    var UserList = Backbone.View.extend({ 
     el: '.page', 
     render: function() { 
     var that = this; 
     var users = new Users(); 
     users.fetch({ 
      success: function() { 
      var template = _.template($('#user-list-template').html(), {users: users.models}) ; 
      that.$el.html(template); 
      } 
     })   
     } 
    }); 

    var Router = Backbone.Router.extend({ 
     routes: { 
     '': 'home' 
     } 
    }); 

    var userList = new UserList(); 

    var router = new Router(); 
    router.on('route:home', function() { 
     userList.render() 
    }); 

    Backbone.history.start(); 

    </script> 
</body> 

回答

1

問題似乎被關閉您的_.each循環。你有<%)}; %>應該是<%}); %>

+0

謝謝你的注意。這是問題所在。啊那些括號.. :) – Andrius

+0

沒問題! :)你的IDE應該給你關於這種類型的東西的警告。減少頭痛! – DanyZift

+0

是的。我認爲我需要更好的工具來看這樣的錯誤:) – Andrius