我可以't填充backbone.js视图
I can't populate a backbone.js view
我在从backbone.js集合填充视图时遇到了问题。
这是我的观点:
<script type="text/template" id="item-template">
<div style="float:left;min-height: 200px;min-width: 50%;">
<div style="float:left;">
<h4>Totals for the day: <%- logdate %></h4>
<p><strong>Total Calories:</strong> <span><%- calories %></span></p>
<p><strong>Total Protein:</strong> <span><%- protein %></span></p>
<p><strong>Total Carbohydrates:</strong> <span><%- carbs %></span></p>
<p><strong>Total Fat:</strong> <span><%- fat %></span></p>
<p><strong>Total Sugar:</strong> <span><%- sugar %></span></p>
</div>
<div style="float:right;min-height: 200px;min-width: 50%;">
</div>
<div style="width: 100%;clear: both;">
<hr>
</div>
</div>
</script>
以下是我的js代码片段:
//the model and collection
var LogEntry = Backbone.Model.extend({
defaults: {
title: 0,
logdate: '',
calories: 0,
fat: 0,
carbs: 0,
protein: 0,
sugar: 0,
content: {}
},
initialize: function() {
/*if (!this.get("title")) {
this.set({"title": this.defaults().title});
}*/
}
});
var FoodJournalCollection = Backbone.Collection.extend({
model: LogEntry,
localStorage: new Backbone.LocalStorage("foodjournal")
});
//setting the body, template, collection variables
el: 'body',
template: _.template($('#item-template').html())
this.journalCollection = new FoodJournalCollection();
//I'm trying to retrieve the the collection and populate the view
this.journalCollection.fetch();
this.$el.html(this.template(this.journalCollection.toJSON()));
这是我得到的错误:ReferenceError:logdate未定义
this.journalCollection.toJSON()
解析为一组模型。
你应该这样做:
this.$el.html(this.template({
models: this.journalCollection.toJSON()
}));
然后在您的模板中:
<script type="text/template" id="item-template">
<% for(var model in models){ %>
// All your HTML ... <%- models[model].calories %> ... etc
<% } %>
</script>
它将使用您定义的模板打印出所有模型信息。
相关文章:
- backbone.js无法渲染视图
- 如何使用backbone.js从集合中获取模型名称
- Backbone.js restful json API design
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- Backbone.js micro templating
- FancyTree JS+Backbone.JS-将数据加载到FancyTree时出现问题
- Backbone.js将模型绑定到视图时出错
- minimy javascript是什么意思?Backbone.js
- Backbone.js视图中的多个模型
- 在哪里存储HTML模板以在Backbone.js中使用
- 带有jquery插件的backbone.js视图
- 在Backbone.js中为窗口对象指定变量
- 如何在Backbone.js中编写可重用的模型和视图
- Backbone.js批量更新集合
- 从模型传递id以查看Backbone.js
- Backbone.js中的名称间距
- 一种将Apache Thrift JS绑定与Backbone.JS一起使用的方法
- 在Backbone.js中将回调绑定到此
- Backbone.js ListenToOnce被调用两次
- 使用Backbone.js访问JSON响应的部分