15

问题

使用Backbone.Marrionette.Layout呈现一些表格数据。表格的<tbody>一部分是Backbone.Marionette.Region,用于显示Backbone.Marionette.CollectionView

我无法弄清楚如何使用 Marionette 的“区域”来做到这一点,而不会通过在元素内插入一个额外的 HTML 元素来弄乱表格显示<tbody>

示例代码

Layout看起来像这样:

Backbone.Marionette.Layout.extend({
    template:...
    regions:{
        list_region: '#list-region'
    }
    onRender:function(){
        var collection = new TheCollection()
        var collectionView = new TheCollectionView({
            collection: collection
        })
        // PROBLEM: The region seems to needs its own HTML element,
        //   and the CollectionView also seems to need its on HTML
        //   element, but as far as I can see, there is only room 
        //    for one element: <tbody>?
        this.list_region.show(collectionView);
});

布局的模板包含整个表格:

<table>

    <tbody id='list-region'>

    </tbody>

    <tfoot id='footer-region'>
        Some other stuff goes here that is not a collection, so I was able 
        to make the View's 'tagName' property 'tr', which worked fine.
    </tfoot>

</table>

有什么建议么?

4

2 回答 2

16

这种布局的目的仅仅是为了方便一张桌子吗?如果是这样,您应该考虑改用 CompositeView。


RowView = Marionette.ItemView.extend({
  tagName: "tr",
  template: ...
});

TableView = Marionette.CompositeView.extend({
  template: ...,

  childView: RowView,

  childViewContainer: "#list-region"
});

差不多就是这样。这会将您的所有 itemViews 呈现到 tbody 中。

于 2012-08-20T20:10:58.213 回答
2

Marionette 3 弃用了CompositeView该类。相反,区域现在可以使用选项el覆盖其内部视图的渲染内容。replaceElement

请参阅此示例以呈现表格:

var RowView = Marionette.View.extend({
  tagName: 'tr',
  template: '#row-template'
});

var TableBody = Marionette.CollectionView.extend({
  tagName: 'tbody',
  childView: RowView
});

var TableView = Marionette.View.extend({
  tagName: 'table',
  className: 'table table-hover',
  template: '#table',

  regions: {
    body: {
      el: 'tbody',
      replaceElement: true
    }
  },

  onRender: function() {
    this.showChildView('body', new TableBody({
      collection: this.collection
    }));
  }
});

var list = new Backbone.Collection([
  {id: 1, text: 'My text'},
  {id: 2, text: 'Another Item'}
]);

var myTable = new TableView({
  collection: list
});

myTable.render();
于 2016-07-10T15:43:41.507 回答