我是一般编码的新手,并且将 Backbone 与 Rails 一起学习。我在模板中迭代收集数据的尝试失败了,我不确定出了什么问题。加载网页时,似乎 json 数据和模板的一部分加载正常,但迭代位失败。这是代码:
收购.js:
var Acquisition = Backbone.Model.extend({
});
收购.js:
var Acquisitions = Backbone.Collection.extend({
model: Acquisition,
url: '/acquisitions.json'
});
收购索引.js:
var AcquisitionsIndex = Backbone.View.extend({
tagName: "table",
render: function() {
this.$el.html(JST['acquisitions/index']({ collection: this.collection }));
return this;
}
});
index.jst.ejs:
<tbody>
<tr>
<th>Cash(USD)</th>
<th>Date</th>
</tr>
<% collection.each(function(model) { %>
<tr>
<td><%= model.escape('cashUSD') %></td>
<td><%= model.escape('date') %></td>
</tr>
<% }); %>
</tbody>
home.html.erb:
<header>
<h1>Startup acquisitions</h1>
</header>
<div id="app"></div>
<script>
var acquisitions = new Acquisitions;
acquisitions.fetch();
var acquisitionsIndex = new AcquisitionsIndex({collection: acquisitions});
acquisitionsIndex.render();
$("#app").append(acquisitionsIndex.el)
</script>