【发布时间】:2015-06-29 07:33:21
【问题描述】:
我正在尝试在空格键中编写自定义迭代器(我使用的是流星 1.1.3)。迭代器将是一个顺序 for 循环(基本上是在需要时替换我对 #each 的使用,因为我相信 #each 不能保证在其迭代中是顺序的)。
我尝试了以下方法:
在库中 -
UI.registerHelper 'sequentialFor', () ->
ret = ""
for i in [0...@.length]
id = @[i]
ret = ret + Template.noop
ret
noop.html -
<template name="noop">
{{> UI.contentBlock this}}
<template>
main.html -
{{#sequentialFor ids}}
<div id="wow-{{this}}">stuff</div>
{{/sequentialFor}}
上面的 ids 是一个从 main 的模板助手传递过来的字符串数组。
现在它抱怨我的 UI 助手返回的是 [object Object] [object Object]。 为了理智,我知道如果我将 UI 助手替换为:
UI.registerHelper 'sequentialFor', () ->
//ret = ""
//for i in [0...@.length]
// id = @[i]
// ret = ret + template
id = @[0]
Template.noop
我知道 main.html 中的 div 会根据需要显示适当的 id 作为其 id 属性的一部分。但是,我似乎无法使 for 循环工作。
我不能简单地直接从帮助程序返回 main.html 中的 div,因为我需要用我的新迭代器包装很多 div,每个 div 都有非常不同的属性。
我想一个简单的问题是,如何在空格键中定义我自己的块迭代器(类似于#each)?
更困难的问题可能是,我上面的方法有什么问题?
我考虑了很多资源,但只发现以下内容非常有用: How to pass an object from to a block helper back to the block in meteor blaze? https://github.com/meteor/meteor/wiki/Using-Blaze https://github.com/meteor/meteor/blob/devel/packages/spacebars/README.md Iterating over basic “for” loop using Handlebars.js
注意我正在使用咖啡脚本
【问题讨论】:
-
如果您在 mongo 查询中使用排序,则每个都没有排序?
-
for 循环不起作用,因为
this不是数组。 -
{{#each}}在一个数组上将被排序。 -
@user3374348 据我所知,我不相信 {{#each}} 保证是连续的。请参阅下面我对 pfkurtz 回答的评论,以更好地了解我的担忧。
标签: meteor coffeescript meteor-blaze spacebars