Backbone.js 如何从CompositeView的子级获取数据?
我有一个带有Backbone.js 如何从CompositeView的子级获取数据?,backbone.js,marionette,Backbone.js,Marionette,我有一个带有和的复合视图。我需要添加到的集合中的特定数据,以便在中使用。具体来说,我需要显示集合中记录的总数以及所有型号的总成本(每个型号都有一个带有美元值的amount属性,如54.35) 我该怎么做 以下是所有相关代码: /* * Layout */ var AppLayout = Backbone.Marionette.Layout.extend({ template: "#customer-view", regions: { card: "#custo
和
的复合视图。我需要添加到
的集合中的特定数据,以便在
中使用。具体来说,我需要显示集合中记录的总数以及所有型号的总成本(每个型号都有一个带有美元值的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>
订单历史
订单
$
日期
执照持有人
单位
命令
全部的
和
是我需要插入此数据的地方
谢谢你给我的任何帮助 CompositeView可以有一个集合和一个模型,创建一个具有所需属性的模型,在本例中为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 beforeRender函数,如果在调用区域上的show时这样做,这将调用compositeView的render函数,就是这样。您的模型也将在集合中呈现。然后如何访问模板中的
numberOfRecords
?如果您使用下划线作为模板引擎,只要模型属性的名称与模板中的变量匹配,就可以使用下划线,与将定义为模型值的占位符时的任何其他视图一样。这将呈现该值,但是如果您想要操纵调用HistoryItemsView.model.get('numberOFRecors')的值,将在cae中为您提供其他地方需要的值。非常接近,但它返回的记录数(共12条)为0
。在将模型传递到视图之前,请尝试计算记录数,如果您的模型已设置,我认为它应该可以工作。将其更改为onCompositeCollectionRendered
似乎可以工作。这是个坏主意吗?