我有一个带有<header>
和<tbody>
的CompositeView。我需要添加到<tbody>
的集合中的特定数据,以便在<header>
中使用。具体来说,我需要显示集合中的记录总数以及所有模型的总成本(每个模型都有amount
属性,其中包含美元值,如54.35
)。
我该怎么做?
以下是所有相关代码:
/*
* Layout
*/
var AppLayout = Backbone.Marionette.Layout.extend({
template: "#customer-view",
regions: {
card: "#customer-card",
quotes: "#customer-quotes",
orders: "#customer-order-history"
}
});
Show.Layout = new AppLayout();
Show.Layout.render();
/*
* ItemView
*/
Show.HistoryItemView = Backbone.Marionette.ItemView.extend({
tagName: "tr",
template: "#customer-history-item"
});
Show.HistoryItemsView = Backbone.Marionette.CompositeView.extend({
template: "#customer-history-wrapper",
itemView: Show.HistoryItemView,
itemViewContainer: "tbody"
});
/*
* Items
*/
var customer_orders = Customers.request("customer:orders", UserID);
var customer_orders_view = new Show.HistoryItemsView({
collection: customer_orders
});
Show.Layout.orders.show(customer_orders_view);
......还有模板:
<script type="text/template" id="customer-history-wrapper">
<div class="module collapsible">
<header>
<hgroup>
<h2>Order History</h2>
</hgroup>
<div class="results">
<div class="left"><strong><%= NUMBER OF RECORDS %></strong> Orders</div>
<div class="right">$ <%= TOTAL COST OF RECORDS %></div>
</div>
</header>
<div class="module-content no-pad">
<table class="simple-table six-up" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Date</th>
<th>Licensee</th>
<th>Company</th>
<th>Order</th>
<th class="numeric">Total</th>
<th class="cta"> </th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</script>
<%= NUMBER OF RECORDS %>
和<%= TOTAL COST OF RECORDS %>
是我需要插入此数据的地方。
感谢我能得到的任何帮助!!
答案 0 :(得分:1)
CompositeViews可以有一个集合和一个模型,用你需要的属性创建一个模型,在这种情况下,numberofRecors,totalCost, 然后在你的onBeforeRender函数上计算模型的总数,就像这样。
var customer_orders_view = new Show.HistoryItemsView({
model: totals,
collection: customer_orders
});
Show.Layout.orders.show(customer_orders_view);
Show.HistoryItemsView = Backbone.Marionette.CompositeView.extend({
template: "#customer-history-wrapper",
itemView: Show.HistoryItemView,
itemViewContainer: "tbody",
onBeforeRender : function () {
this.model.set({numberOfRecors : this.collection.length});
//here you can set the model values to be displayed.
}
});
你也可以通过预先计算的值传递模型,你不需要on before before函数,如果你在区域上调用show时这样做,这将调用compositeView的render函数,那将是它。你的模型也将在你的收藏中呈现。