【发布时间】:2012-11-05 23:55:28
【问题描述】:
为什么这在流星中不起作用? https://github.com/wycats/handlebars.js/issues/250
【问题讨论】:
标签: meteor handlebars.js
为什么这在流星中不起作用? https://github.com/wycats/handlebars.js/issues/250
【问题讨论】:
标签: meteor handlebars.js
尚未在流星版本的车把中实现; @index 正确渲染的反应性有一个微妙之处。你可以在这里阅读更多信息:https://github.com/meteor/meteor/issues/489#issuecomment-11270564
【讨论】:
这对我来说绝对是一种挫败感。与此同时,我制作了一个车把助手来将任何内容解析为命名的“键”和“值”对象:
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 直接推送到结果中。
让它工作的另一种方法是使用标准 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;
};
【讨论】: