【问题标题】:How to create BIll or invoice view in backbone marionette?如何在骨干木偶中创建账单或发票视图?
【发布时间】:2014-05-15 09:37:27
【问题描述】:

我是 Marionette 的初学者,正在尝试使用这些数据创建呈现发票/计费模板的视图:

    data={
        name: "jems"
        date: "may 15 2014"
        dataList:[
            {
                date: "may 15 2014",
                product: "xyz",
                qty: "3",
                rate: "5",
                totalAmount: "5"
            },
            {
                date: "may 15 2014",
                product: "xyz",
                qty: "3",
                rate: "5",
                totalAmount: "5"
            },
            {
                date: "may 15 2014",
                product: "xyz",
                qty: "3",
                rate: "5",
                totalAmount: "5"
            },
            {
                date: "may 15 2014",
                product: "xyz",
                qty: "3",
                rate: "5",
                totalAmount: "5"
            }
        ]
    }

我尝试使用来自http://jsfiddle.net/derickbailey/xX9X3/ 示例的复合视图。但没有运气。

使用collectionView、compositeView、itemView 来创建嵌套发票类型视图有点困惑。在一些嵌套视图示例中,我还发现使用的布局让我完全困惑。

请帮忙!

提前致谢!

【问题讨论】:

    标签: backbone.js marionette backbone-views


    【解决方案1】:

    您的数据没有小提琴示例中的数据复杂。您有 1 个模型包含一个集合。根据我的经验,这需要为每个集合成员组合 CompositeView + ItemView。

    <script id="invoice-template" type="text/template">
    <%= name %> on <%= date %>:
    
    <ul></ul>
    </script>
    
    <script id="billing-template" type="text/template">
        <%= date %>, <%= totalAmount %>$ for product: <%= product %>
    </script>    
    
    var BillingView = Backbone.Marionette.ItemView.extend({
        template: "#billing-template",
        tagName: "li"
    });
    
    var InvoiceView = Backbone.Marionette.CompositeView.extend({
        template: "#invoice-template",
    
        tagName: "ul",
        itemView: BillingView    
    });
    

    在这里查看实际操作:http://jsfiddle.net/xX9X3/116/

    【讨论】:

    • 感谢@Tumas!还有一个问题,我可以在一个复合视图中使用多个 itemView 吗?例如:如果有两个 dataList(dataList1 和 dataList2)都呈现在两个不同的表或模板中。
    • 我认为您可以进行一些手动调整,但我建议对另一个集合使用另一个视图。如果说 dataList2 更改而 dataList1 没有更改,这将更加灵活和高效 - 您不需要重新渲染两个集合的视图,只需重新呈现集合已更改的视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多