0

我有一个带有 a<header>和 a的 CompositeView <tbody>。我需要集合中的特定数据,这些数据添加<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">&nbsp;</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</script>

<%= NUMBER OF RECORDS %>并且<%= TOTAL COST OF RECORDS %>是我需要插入这些数据的地方。

感谢我能得到的任何帮助!!

4

1 回答 1

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 beforeRender 函数,如果这样做,当您在区域上调用 show 时,这将调用 CompositeView 的渲染函数,就是这样。您的模型也将与您的收藏一起呈现。

于 2013-05-31T15:54:48.947 回答