我开始学习Backbone,但是UnderscoreJS似乎在这里给我一些问题。我正在获得"意想不到的令牌。"而且我不确定这意味着什么。我的JSON看起来很好,所以我不希望它成为一个原因。
var Items = Backbone.Collection.extend({
url: 'http://welfordian.com/backbone/data/items'
});
var ToDoList = Backbone.View.extend({
el: '.page',
render: function() {
var items = new Items();
var elem = this;
items.fetch({
success: function(items) {
var template = _.template($('#todo-list-content').html(), {
items: items.models
});
elem.$el.html(template);
}
});
}
});
var Router = Backbone.Router.extend({
routes: {
'': 'index',
'completed': 'completed'
}
});
var toDo = new ToDoList({});
var router = new Router();
router.on("route:index", function() {
toDo.render();
});
Backbone.history.start();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js"></script>
<div class="page"></div>
<script type="text/html" id="todo-list-content">
<table>
<thead>
<tr>
<th>ID</th>
<th>Description</th>
<th>State</th>
</tr>
</thead>
<tbody>
<% ._each(items, function(item){ %>
<tr>
<td>Hello</td>
</tr>
<% }); %>
</tbody>
</table>
</script>
&#13;
答案 0 :(得分:3)
模板中each
的语法不正确。
更改
._each(items, function(item){
^^
到
_.each(items, function(item) {