指定一个HTML表的 元件作为用于在Backbone.js的木偶的区域(Specify an

2019-07-31 02:45发布

问题

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

我无法弄清楚如何在不插入内部的额外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>

有什么建议?

Answer 1:

是这种布局的意图仅仅是为了便于表? 如果是这样,你应该看看使用CompositeView中代替。


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

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

  childView: RowView,

  childViewContainer: "#list-region"
});

这几乎是它。 这将使得所有的itemViews的到TBODY。



Answer 2:

木偶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();


文章来源: Specify an HTML table's element as a region in Marionette for Backbone.js