如何在BackboneJS*marionette*collectionView中打印行线?

Guy*_*Guy 2 javascript backbone.js marionette

我有一个使用LI itemView创建UL的collectionView.

我想在下划线模板中使用项索引号(count).即:

hello (item 0)
world (item 1)
Run Code Online (Sandbox Code Playgroud)

有人知道如何使用牵线木偶计数吗?我想避免把它放在模型中.

这就是我希望我的itemView模板看起来像(项目数为n):

<script id="task-template" type="text/html">
          <div class="order"><%=n%></div>
          <div class="title-container">
               <a href="#">...</a>
          </div>
 </script>
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏,

干杯,

zhi*_*eng 9

我刚刚找到了一个简单的方法.(使用Marionette v1.0.0-rc6)

使用templateHelpers属性.

在商品视图中:

MyItemView = Backbone.Marionette.ItemView.extend({
    template: "#my-item-view-template",

    templateHelpers: function(){

        var modelIndex = this.model.collection.indexOf(this.model);
        return {
            index: modelIndex
        }

    }
});
Run Code Online (Sandbox Code Playgroud)

在模板中,您可以使用以下命令打印索引:

<%= index %>
Run Code Online (Sandbox Code Playgroud)

就这样.