gpt4 book ai didi

backbone.js - 如何在 Marionette 3+ 中实现已弃用的 CompositeView 功能?

转载 作者:行者123 更新时间:2023-12-02 21:53:28 25 4
gpt4 key购买 nike

如最新Marionette docs所述:

CompositeView is deprecated. You should use the replaceElement option on Region.show and render a CollectionView into a region inside a View to achieve this functionality.

我现在仍然不明白 CompositeView 功能应该如何实现。

以前,CompositeView 非常适合与此类模板一起使用:

<script id="table-template" type="text/html">
<table>
<% if (items.length) { %>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<% } %>

<tbody></tbody>

<tfoot>
<tr>
<td colspan="3">some footer information</td>
</tr>
</tfoot>
</table>

new MyCompositeView({
template: "#table-template",
templateContext: function() {
return { items: this.collection.toJSON() };
}
// ... other options
});

如果我们决定使用 LayoutView 而不是 CompositeView,那么我们需要手动编写大量事件绑定(bind)代码(例如,根据数量显示/隐藏表头)收藏中的元素)。这让事情变得更加困难。

没有 CompositeView 是否有干净且不复杂的生活方式?

<小时/>

感谢您的帮助或建议。

最佳答案

看起来 Marionette 3 将去掉一些概念,使框架整体更简单,更容易理解。

Marionette.View 3 将包含 ItemView 和 LayoutView 的功能。 CompositeView 已被弃用,转而只使用 RegionManager,它现已包含在 View 中。

v2 --> v3
View -> AbstractView
ItemView, LayoutView -> View

这是一个快速示例应用程序:

var color_data = [ { title:'red' }, { title:'green' }, { title:'blue' } ];

var Color = Backbone.Model.extend({
defaults: { title: '' }
});

var Colors = Backbone.Collection.extend({
model: Color
});

var ColorView = Mn.View.extend({
tagName: 'tr',
template: '#colorTpl'
});

var ColorsView = Mn.CollectionView.extend({
tagName: 'tbody',
childView: ColorView
});

var AppView = Mn.View.extend({
template: '#appTpl',
templateContext: function(){
return {
items: this.collection.toJSON()
};
},
ui: {
input: '.input'
},
regions: {
list: {
selector: 'tbody',
replaceElement: true
},
},
onRender: function(){
this.getRegion('list').show(new ColorsView({
collection: this.collection
}));
},
events: {
'submit form': 'onSubmit'
},
onSubmit: function(e){
e.preventDefault();
this.collection.add({
title: this.ui.input.val()
});
this.ui.input.val('');
}
});

var appView = new AppView({
collection: new Colors(color_data)
});
appView.render().$el.appendTo(document.body);
<script src='http://libjs.surge.sh/jquery2.2.2-underscore1.8.3-backbone1.3.2-radio1.0.4-babysitter0.1.11-marionette3rc1.js'></script>

<script id="colorTpl" type="text/template">
<td><%=title%></td>
<td style="background-color:<%=title%>">&nbsp;</td>
</script>

<script id="appTpl" type="text/template">
<table width="100%">
<% if(items.length) { %>
<thead>
<tr>
<th width="1%">Title</th>
<th>Color</th>
</tr>
</thead>
<% } %>
<tbody></tbody>
<tfoot>
<tr>
<td colspan="2">
<form><input type="text" class="input" autofocus><input type="submit" value="Add Color"></form>
</td>
</tr>
</tfoot>
</table>


</script>

关于backbone.js - 如何在 Marionette 3+ 中实现已弃用的 CompositeView 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36341918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com