我正在嘗試在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