【问题标题】:How to achieve deprecated CompositeView functionality in Marionette 3+?如何在 Marionette 3+ 中实现已弃用的 CompositeView 功能?
【发布时间】:2016-03-31 19:21:01
【问题描述】:

如最新Marionette docs中所述:

CompositeView 已弃用。您应该在Region.show 上使用replaceElement 选项和 将CollectionView 渲染到View 内的区域中以实现此功能。

我还是不明白现在应该如何实现CompositeView 功能。

以前,CompositeView 非常适合与此类模板一起使用:

<script id="table-template" type="text/html">
<table>
  <% if (items.length) { %>
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
 <% } %>

  <tbody></tbody>

  <tfoot>
    <tr>
      <td colspan="3">some footer information</td>
    </tr>
  </tfoot>
</table>

new MyCompositeView({
  template: "#table-template",
  templateContext: function() {
    return { items: this.collection.toJSON() };
  }
  // ... other options
});

如果我们决定使用LayoutView 而不是CompositeView,那么我们需要手动编写大量事件绑定(例如根据集合中的项目数显示/隐藏表头)。这让事情变得更加困难。

没有CompositeView,还有什么干净而不复杂的生活方式吗?


感谢您的任何帮助或建议。

【问题讨论】:

    标签: backbone.js handlebars.js marionette underscore.js-templating


    【解决方案1】:

    看起来,Marionette 3 将摆脱一些概念,使框架整体更简单,更容易理解。

    3 中的 Marionette.View 将包含来自 ItemView 和 LayoutView 的功能。 CompositeView 已被弃用,取而代之的是仅使用 RegionManager,它现在包含在 View 中。

    v2 --> v3
    View -> AbstractView  
    ItemView, LayoutView -> View
    

    这是一个简单的示例应用:

    var color_data = [ { title:'red' }, { title:'green' }, { title:'blue' } ];
    
    var Color = Backbone.Model.extend({
      defaults: { title: '' }
    });
    
    var Colors = Backbone.Collection.extend({
      model: Color
    });
    
    var ColorView = Mn.View.extend({
      tagName: 'tr',
      template: '#colorTpl'
    });
    
    var ColorsView = Mn.CollectionView.extend({
      tagName: 'tbody',
      childView: ColorView
    });
    
    var AppView = Mn.View.extend({
      template: '#appTpl',
      templateContext: function(){
        return {
          items: this.collection.toJSON()
        };
      },
      ui: {
        input: '.input'
      },
      regions: {
        list: {
          selector: 'tbody',
          replaceElement: true
        },
      },
      onRender: function(){
        this.getRegion('list').show(new ColorsView({
          collection: this.collection
        }));
      },
      events: {
        'submit form': 'onSubmit'
      },
      onSubmit: function(e){
        e.preventDefault();
        this.collection.add({
          title: this.ui.input.val()
        });
        this.ui.input.val('');
      }
    });
    
    var appView = new AppView({
      collection: new Colors(color_data)
    });
    appView.render().$el.appendTo(document.body);
    <script src='http://libjs.surge.sh/jquery2.2.2-underscore1.8.3-backbone1.3.2-radio1.0.4-babysitter0.1.11-marionette3rc1.js'></script>
    
    <script id="colorTpl" type="text/template">
      <td><%=title%></td>
      <td style="background-color:<%=title%>">&nbsp;</td>
    </script>
    
    <script id="appTpl" type="text/template">
    <table width="100%">
      <% if(items.length) { %>
        <thead>
          <tr>
            <th width="1%">Title</th>
            <th>Color</th>
          </tr>
        </thead>
      <% } %>
      <tbody></tbody>
      <tfoot>
        <tr>
          <td colspan="2">
            <form><input type="text" class="input" autofocus><input type="submit" value="Add Color"></form>
          </td>
        </tr>
      </tfoot>
    </table>
    
      
    </script>

    【讨论】:

    • 太好了,非常感谢您提供的精彩示例!它解决了这个问题。
    • 最后我发现Marionette中没有replaceElement: true这样的选项。你为什么用它?
    • Jamesh,我们正在谈论 Marionette v3 和 replaceElement 选项 is recommended for use instead of CompositeView。还是might not be entirely supported,但你的说法不正确。
    • replaceElement: true 是实现类似于 CompositeView 中的 childViewContainer 的行为所必需的。否则,默认情况下,该区域本身就是一个容器,这将是 DOM 树中的一个额外元素。默认区域行为会使其在像这样的示例中变得更加困难,在表格中,它对 DOM 元素的结构很敏感。
    • 我很抱歉。我在想replaceElement: true 在 Marionette 3 中尚不可用。但那是错误的,你的答案是完全正确的!再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    相关资源
    最近更新 更多