如何使用CompositeView在模板中添加静态html内容
Backbone.Marionette - How to add static html content in template using CompositeView?
我是Marionette的新手。我开始了解Marionette复合视图。我不知道我是错了还是写了。
我的问题是,如何在模板上保留一些静态的html内容?
我的复合视图模板:
<script type="text/template" id="angry_cats-template">
<div><a href="#logout">Click to logout</a></div> // i would like to add some static html here.
<thead>
<tr class='header'><th>Name</th></tr>
</thead>
<tbody></tbody>
</script>
<script type="text/template" id="angry_cat-template">
<td><%= name %></td>
</script>
我是这样渲染的:
myApp = new Backbone.Marionette.Application();
myApp.addRegions({
mainRegion:"#content"
});
AngryCat = Backbone.Model.extend({});
AngryCats = Backbone.Collection.extend({
model:AngryCat
});
AngryCatView = Backbone.Marionette.ItemView.extend({
template:"#angry_cat-template",
tagName:'tr',
className:'angry_cat'
});
AngryCatsView = Backbone.Marionette.CompositeView.extend({
tagName:"table", //how to avoid my logout should not append with table?
id:"angry_cats",
className: "table-striped table-bordered",
template: "#angry_cats-template",
itemView: AngryCatView,
appendHtml: function(collectionView, itemView){
collectionView.$("tbody").append(itemView.el);
}
});
myApp.addInitializer(function(options){
var cats = new AngryCats([
{ name: 'Wet Cat' },
{ name: 'Bitey Cat' },
{ name: 'Surprised Cat' }
]);
var angryCatsView = new AngryCatsView({
collection: cats
});
myApp.mainRegion.show(angryCatsView);
});
$(document).ready(function(){
myApp.start();
});
有人帮我吗?
实时演示
您必须从CompositeView
中删除className
和tagName
,并按照以下更新复合模板
<div><a href="#logout">Click to logout</a></div> // Now i am out.
<table class="table-striped table-bordered">
<thead>
<tr class='header'><th>Name</th></tr>
</thead>
<tbody></tbody>
</table>
此外,您不需要在CompositeView中具有appendHtml
。取而代之的是简单的集合itemViewContainer: "tbody"
,其中项目视图将被附加。
请结账http://jsfiddle.net/fizerkhan/y6nH5/
您的错误可能在AngryCatsView appendHtml:function()中
您不应该需要此功能
要为Marionette指定要将子元素渲染到的div,请使用
itemView: AngryCatView
itemViewContainer: "tbody"
我写了一篇关于在TypeScript中使用Marionette的教程风格的博客。
相关文章:
- 如何将静态字符串添加到受信任的 ng-src
- 如何将活动类添加到第一个动态选项卡而不是角度的静态选项卡
- 在sharepoint页面中的静态行上添加一条水平线
- 使用 Knockout.js 在文本区域中添加数据绑定和静态文本
- 如何在 AngularJS 中生成的模板中添加静态 HTML
- 丰富的面孔 :如何在输入数字滑块工具提示的值旁边添加静态文本
- 如何将静态文本添加到输入值
- 添加<样式>并且<脚本>在Magento静态块中
- Javascript:根据用户输入添加到静态URL;添加#xyz
- 静态贴图API添加色调|饱和度|亮度会破坏图像
- Javascript-使用静态变量添加点击侦听器
- 在javascript中添加静态资源的URL路径(在Spring MVC中)
- 如何将过期标头添加到Meteor静态资产中
- servlet过滤器中的静态资源版本控制(添加一个额外的GET参数)
- CSS效果可以很好地处理静态表数据,但当通过javascript添加表数据时就不行了
- jQuery添加动态单元格到一个静态单元格到表格中
- 向html中添加静态内容表
- 主干:添加静态json文件到集合时出错
- 在制作动态表时添加静态标头名称
- 如何使用CompositeView在模板中添加静态html内容