在meteor中使用@index #each迭代器不起作用

时间:2012-11-17 12:14:48

标签: meteor handlebars.js

为什么这不适用于流星? https://github.com/wycats/handlebars.js/issues/250

3 个答案:

答案 0 :(得分:9)

尚未在流星版的车把中实施;关于@index正确渲染的反应性有一个微妙的。您可以在此处详细了解:https://github.com/meteor/meteor/issues/489#issuecomment-11270564

答案 1 :(得分:9)

这对我来说绝对是一种挫败感。与此同时,我制作了一个把手助手来解析任何名为'key'和'value'的对象:

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>

答案 2 :(得分:6)

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

这是一个示例,说明如何在将每个索引与集合一起使用时返回索引:

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;
};