0

と を含む CompositeView が<header>あり<tbody>ます。<tbody>で使用するために に追加されたコレクションから特定のデータが必要<header>です。具体的には、コレクション内のレコードの TOTAL NUMBER と、すべてのモデルの TOTAL COST を表示する必要があります (各モデルには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のrender関数が呼び出され、それになります。モデルもコレクションとともにレンダリングされます。

于 2013-05-31T15:54:48.947 に答える