2015-04-21 130 views
2

我正在尝试在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

+0

每个都没有排序,如果你在你的mongo查询中使用排序? –

+0

for循环不起作用,因为'this'不是数组。 – pfkurtz

+0

数组上的'{{#each}}'将被排序。 – user3374348

回答

0

看样子对我来说,你想为每个ID数组创建一个<div>(纠正我,如果我错了)。这是我会怎么做呢,没有必要的自定义迭代器:

Template.registerHelper('ids', function(arrayWithIds) { 
    if (!arrayWithIds) return []; 
    // do some sorting or whatever with arrayWithIds, for example: 
    var arrayOfIds = _.map(arrayWithIds, function(obj) { 
     return obj._id; 
    }); 
    return arrayOfIds; 
}); 

然后在main.html中:

{{#each ids someDataSetWithIds}} 
    // `someDataSetWithIds` is the helper's parameter 
    // `this` in each case is an ID 
    <div id="wow-{{this}}"></div> 
{{/each}} 

如果你的助手返回一个对象,你会在模板中使用this._id,而不是。我误解了你想要达到的目标吗?

+0

我很欣赏答案,但这并不能解决我所问的问题。我已经可以像您在答案中一样创建这些div(每个ID一个)。我试图解决的问题是(据我所知)spacebars并不能保证#each将始终是同步的(即使使用它的当前实现)。保证顺序迭代对我的应用程序非常重要,所以我正在尝试编写一个顺序迭代器来代替spacebars中找到的标准#each迭代器。 –

+0

即使我创建这个迭代器的原因看起来是偏执狂,但从一般人如何创建/重新创建空间条迭代器的角度来看,我可以自由地处理我的问题。{{#each}} –

1

我设法使用类似于你可能会在Haskell使用什么或Lisp的递归技术获得一个自定义的迭代器:

<body> 
    {{#countdown n=5}} 
    <p>item {{this}}</p> 
    {{/countdown}} 
</body> 

<template name="countdown"> 
    {{#if positive}} 
    {{> Template.contentBlock n}} 
    {{#countdown n=nMinusOne}} 
     {{> Template.contentBlock this}} 
    {{/countdown}} 
    {{/if}} 
</template> 
Template.countdown.helpers({ 
    positive: function() {return this.n > 0;}, 
    nMinusOne: function() {return this.n - 1;} 
}); 

meteorpad

性能可能比通常的{{#each}}差很多。

+0

感谢您的详细解答。我认为这是最好的。然而,我不打算将它标记为解决方案,直到我确定没有一种标准的方式来编写自定义迭代器(就像以前那样)。无论哪种方式都有投票权。 –