【问题标题】:Using @index in meteor #each iterator doesn't work [duplicate]在流星#each迭代器中使用@index不起作用[重复]
【发布时间】:2012-11-05 23:55:28
【问题描述】:

为什么这在流星中不起作用? https://github.com/wycats/handlebars.js/issues/250

【问题讨论】:

标签: meteor handlebars.js


【解决方案1】:

尚未在流星版本的车把中实现; @index 正确渲染的反应性有一个微妙之处。你可以在这里阅读更多信息:https://github.com/meteor/meteor/issues/489#issuecomment-11270564

【讨论】:

    【解决方案2】:

    这对我来说绝对是一种挫败感。与此同时,我制作了一个车把助手来将任何内容解析为命名的“键”和“值”对象:

    Handlebars.registerHelper('key_value', function(context, options) {
      var result = [];
      _.each(context, function(value, key, list){
        result.push({key:key, value:value});
      })
      return result;
    });
    

    这将与#each 运算符一起使用,例如:

    <dl class="attributes">
      {{#each key_value attributes}}
        <dt>{{key}}</dt><dd>{{value}}</dd>
      {{/each}}
    </dl>
    

    【讨论】:

    • 这非常有效。我用这个稍加修改。我只需将_key 字段添加到value 并将value 直接推送到结果中。
    【解决方案3】:

    让它工作的另一种方法是使用标准 Meteor 模板助手和 map cursor function

    下面是一个示例,展示了如何在将 each 与集合一起使用时返回索引:

    index.html:
    
    <template name="print_collection_indices">
      {{#each items}}
        index: {{ this.index }}
      {{/each}}
    
    index.js:
    
    Items = new Meteor.Collection('items');
    
    Template.print_collection_indices.items = function() {
      var items = Items.find().map(function(doc, index, cursor) {
        var i = _.extend(doc, {index: index});
        return i;
      });
      return items;
    };
    

    【讨论】:

    • map() 方法返回一个数组而不是一个光标,这样你就失去了反应...
    猜你喜欢
    • 2014-06-06
    • 2016-07-31
    • 2021-08-10
    • 2013-02-08
    • 1970-01-01
    • 2015-11-30
    • 2016-05-16
    • 2015-12-20
    • 2018-08-08
    相关资源
    最近更新 更多