使用 Meteor/Blaze 重复依赖于整数的块

Repeat a block dependent on an integer using Meteor/Blaze

我的 mongo 集合中有一个整数来定义计数,称为 'tweet_count'。我需要根据这个值重复一个图标,像这样:

帮手:

Template.companyList.helpers({
    companies: function () {
        return Tweets.find();
    }
});

火焰:

{{#each companies}}
    {{ LOOP BASED ON tweet_count}}
       <i class="fa fa-circle"></i>
    {{ /LOOP }}
{{/each}}

Mongo 集合的样本如下所示:

{
"_id" : "xxx",
"name" : "xxx",
"handle" : "xxx",
"tweet_count" : 2,
"tweets" : [
    {
        "tweet_id" : "x",
        "text" : "x",
        "created_at" : "Tue Jul 04 15:56:33 +0000 2017",
        "retweet_count" : 0,
        "from" : "x",
        "from_full_name" : "x",
        "from_profile_image" : "x"
    },
    {
        "tweet_id" : "x",
        "text" : "x9",
        "created_at" : "Tue Jul 04 15:56:47 +0000 2017",
        "retweet_count" : 0,
        "from" : "x",
        "from_full_name" : "x",
        "from_profile_image" : "x"
    }
]
}

如何使用 Meteor/Blaze 实现此目的?我试过 each 但它只接受一个我没有这个值的数组,因为它只是一个数字。

只需创建一个助手,returns 一个大小为 tweet_count 的数组:

Template.companyList.helpers({
    companies: function () {
        return Tweets.find();
    },
    getTweetCount(tweet_count) {
        const ret = [];
        for (let i = 0; i < tweet_count; i++) {
            ret.push(i);
        }
        return ret;
    }
});

它接收推文计数和 returns 具有推文计数大小的空数组。您可以通过以下方式从 Blaze 语法调用它:

<template name="companyList">
    {{#each companies}}
        <span>{{this.name}}</span>
        {{#each getTweetCount this.tweet_count}}
            <i class="fa fa-circle"></i>
        {{/each}}
    {{/each}}
</template>

this 指的是当前文档,因此 this.tweet_count 是传递给辅助函数 getTweetCount 的数字。