我正在尝试在spacebars中编写自定义迭代器(我正在使用流星1.1.3)。迭代器将成为一个循环顺序(基本上是为了在需要时替换#each的使用,因为我相信#each在迭代中不能保证是连续的)。为Spacebars编写自定义迭代器
我曾尝试以下:
在LIB -
UI.registerHelper 'sequentialFor',() ->
ret = ""
for i in [[email protected]]
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在上述是通过f的字符串数组rom主要的模板助手之一。
现在它抱怨我的UI帮助器的返回是[object Object] [object Object]。 对于理智的缘故,我知道,如果我取代我的UI帮手:
UI.registerHelper 'sequentialFor',() ->
//ret = ""
//for i in [[email protected]]
// id = @[i]
// ret = ret + template
id = @[0]
Template.noop
我得到所希望在我的main.html中的股利显示了相应的ID作为其id属性的一部分。但是,我似乎无法使for循环工作。
我不能直接从helper返回main.html中的div,因为我有很多div需要用新的iterator包装,每个div都有不同的属性。
我想简单的问题是,我如何在spacebars中定义自己的块迭代器(类似于#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
注意我使用的CoffeeScript
每个都没有排序,如果你在你的mongo查询中使用排序? –
for循环不起作用,因为'this'不是数组。 – pfkurtz
数组上的'{{#each}}'将被排序。 – user3374348