代码之家  ›  专栏  ›  技术社区  ›  trh88

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

  •  0
  • trh88  · 技术社区  · 7 年前

    我在mongo集合中有一个整数来定义一个计数,称为“tweet\u count”。我需要基于此值重复一个图标,如下所示:

    帮手:

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

    火焰:

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

    {
    "_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"
        }
    ]
    }
    

    each 但它只接受一个数组,我没有这个值,因为它只是一个数字。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Jankapunkt    7 年前

    只需创建一个助手,返回一个大小为tweet\u 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;
        }
    });
    

    它接收推文计数并返回推文计数大小的空数组。您可以通过以下方式从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