Undercore模板不是't使用Backbone.js进行渲染数据不是't传递到模板
Underscore Template isn't rendering with Backbone.js Data isn't being passed to template
我一直在努力弄清楚模板如何与模型和集合协同工作。教程的某些部分是有意义的,但其他部分则不然。所以我一直在搞JSFiddle,试图让下面的例子发挥作用。
我真正想做的就是构建几个对象。然后将它们输出到特定分区中的表中。
根据这个错误,数据几乎没有被传递到模板中。根据我的理解,我所做的应该是有效的。
var Note = Backbone.Model.extend({
defaults : {
title: "",
description: ""
}
});
var note1 = new Note({title: "Patience", description: "Something we all need"});
var note2 = new Note({title: "Fun Times", description: "All the things"});
var Notebook = Backbone.Model.extend({
model: Note
});
notes = new Notebook([note1, note2]);
var NoteView = Backbone.View.extend({
el: '.content',
initialize: function() {
alert("hello");
this.render();
},
render: function () {
var template = _.template($('#notes-templates').html(), {notes: notes.models});
this.$el.html(template);
}
});
new NoteView();
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<div class="content">
</div>
<script type="text/template" id="notes-templates">
<table>
<thead>
<tr>
<th>title</th>
<th>scripture</th>
</tr>
</thead>
<tbody>
<% _.each(notes, function(note) { %>
<tr>
<td><%= note.get('title') %></td>
<td><%= note.get('description') %></td>
</tr>
<% }); %>
</tbody>
</table>
</script>
尝试将Notebook
作为主干集合,并使用集合api在视图中迭代。也发布在http://jsfiddle.net/rossta/vn8hh5o7/2/
var Note = Backbone.Model.extend({
defaults : {
title: "",
description: ""
}
});
var note1 = new Note({title: "Patience", description: "Something we all need"});
var note2 = new Note({title: "Fun Times", description: "All the things"});
var Notebook = Backbone.Collection.extend({
model: Note
});
notes = new Notebook([note1, note2]);
var NoteView = Backbone.View.extend({
el: '.content',
initialize: function() {
alert("hello");
this.render();
},
render: function () {
var template = _.template($('#notes-templates').html(), {notes: notes});
this.$el.html(template);
}
});
new NoteView();
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<div class="content">
</div>
<script type="text/template" id="notes-templates">
<table>
<thead>
<tr>
<th>title</th>
<th>scripture</th>
</tr>
</thead>
<tbody>
<% notes.forEach(function(note) { %>
<tr>
<td><%= note.get('title') %></td>
<td><%= note.get('description') %></td>
</tr>
<% }); %>
</tbody>
</table>
</script>
相关文章:
- 对API数据使用声明性绑定
- 异步获取数据使用JavaScript同步获取数据
- 如何在字符串中的值之前删除字符串中的动态数据?/ 使用 Lodash 的 _.trimStart 与动态数据
- 如何在IE CORS中发送POST数据(使用XDomainRequest)
- <img src=“;数据:..使用gzip文件
- ng使用angular中的json数据使用optgroup重复select
- KO网格无法保存编辑单元格中的数据-使用Plunker
- 使用C3.js对不同的数据使用相同的json键
- 如何从html元素中获取javascript var数据?(使用canvasjs)
- 对从Ajax返回的数据使用Jquery方法,不打印数据
- 对 svg 数据使用
- JSON 数据 - 使用 Javascript/Jquery 按星期几(周日、周一、周二、周三、周四、周五、周六)分组
- 通过PHP将动态mp3数据使用到嵌入式播放器中不起作用
- 如何在两个
之间共享数据?使用AngularJS和温泉UI - 如何从图表谷歌脚本的电子表格中检索数据.(使用 HTML 服务)
- 保存Chrome扩展程序数据(使用LocalForage)
- 如何在 AngularJS 中对来自另一个范围的数据使用过滤器
- 我的 JSON 数据使用 AJAX 加载了两次,它只应该在单击时加载一次
- 如何使用表单中的数据使用 js 制作表格
- 我如何通过jsonencode数据使用Javascript选择下拉值