0

私は index: {{#eachIndex}} でそれぞれを実装しようとしていますが、私が見つけたすべてのコード例は非推奨のようです:

UI.registerHelper('eachIndex', function () {
    var withIndex = _.map(this, function(x, i) {
        x.index = i;
        return x;
    });
    return UI.Each(function() {
        return withIndex;
    }, UI.block(null), UI.block(null));
});

UI.block は未定義です。

テンプレートにパラメーターを渡そうとする別のスニペットがありますが、それも失敗するため、完全に間違っていると思います。

UI.registerHelper('clearfix', function () {
    return UI.With((function() {
        return {attributes:{class:this.col}};
    }), Template._clearfix);
});
4

1 に答える 1

0

最初のヘルパーでは、UI.each を明示的に呼び出す必要はありません。むしろ、(インデックスと共に) データを返し、ハンドルバーを使用してテンプレートでそれらを反復処理する必要があります。例えば:

UI.registerHelper('eachIndex', function () {
    var items = Items.find().fetch();
    var withIndex = _.map(items, function(x, i) {
        x.index = i;
        return x;
    });
    return withIndex;
});

そしてテンプレートで:

{{#each eachIndex}}
    {{someProperty}}
    {{index}}
{{/each}}

パラメータをテンプレートに渡すのは次のように簡単です。

{{> templateName var=someVar}}
于 2014-10-11T22:07:03.107 に答える