【发布时间】:2015-11-02 13:38:39
【问题描述】:
我有一个itemInCollection = new Meteor.Collection('stuff') 集合,我使用itemInCollection.find() 来获取其中的所有项目。现在我遍历生成的光标以在模板中显示name 属性。
<head>
<title>hello</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> hello}}
</body>
<template name="hello">
<button>Click Me</button>
{{#each item}}
{{counter}} : {{name}}
{{/each}}
</template>
现在我只想在名称前表示一个数字,例如
1. John
2. Doe
3. Darling
counter如何在helper函数中实现?我尝试了以下方法:
Template.hello.helpers({
'item': function() {
return itemInCollection.find();
},
'counter': function() {
var counter = PrimerList.find().count(),
arr = [];
for (var i = 0; i < counter; i++) {
arr.push( i + 1 );
}
return arr;
}
});
在模板中我写了这个:
{{#each item}}
{{#each counter}} {{this}} {{/each}} : {{name}}
{{/each}}
但这给了我这样的感觉:
1 2 3 John
1 2 3 Doe
1 2 3 Darling
【问题讨论】:
标签: javascript meteor spacebars meteor-helper