如何为Meteor / Blaze中的多维数组生成的DIV分配唯一ID

时间:2016-11-17 21:33:31

标签: meteor meteor-blaze

我的页面上每个div都需要一个唯一的ID。现在我正在使用Random.id()在帮助器中分配它,但我刚刚意识到id在rerender上发生了变化(呃!)。

如何获取静态唯一ID?我知道我需要一个柜台,但我不知道怎么用Blaze做。

这是我的HTML:

<template name="article">
    <article>
        <h2>{{title}}</h2>
            {{#each paragraphs}}
                {{> paragraph }}
            {{/each}}
    </article>
</template>
<template name="paragraph">
    <div class="container paragraph">
        {{#each sentences}}
            {{>sentence}}
        {{/each}}
    </div>
    <BR><BR>
</template>
<template name="sentence">
    <span>
        {{#each forward.words }}
            {{> word }}
        {{/each}}
    </span>
</template>
<template name="word">
    {{#with theWord}}
        <div id="{{divID}}" class="word nonPopped" data-wordID="{{_id}}">
            <p class="pinyin thinnerBottom text-muted score{{score}}">{{pinyins.[0].pīnyīn}}</p>
            <h3 class="thinnerTop ">
                <a style="color: black;" href="#">
                    {{#each characters}}
                        {{>character}}
                    {{/each}}
                </a>
            </h3>
        </div>
    {{/with}}
</template>
<template name="character">
    {{#with theCharacter}}
        <span id="{{spanID}}" class="character thinnerTop">{{simplified}}</span>
    {{/with}}
</template>

2 个答案:

答案 0 :(得分:0)

如果您要对某个集合进行迭代,请使用id="{{_id}}。该计数器为{{@index}}

答案 1 :(得分:0)

这似乎有效:

Template.word.onCreated(function () {
    this.data.divID = Random.id(6)
})

Template.word.helpers({
theWord: function () {
    var theWord = Words.findOne({_id: this.wordID})
...
        if (!theWord.divID) {
            theWord.divID = this.divID
        }
        return theWord
    }
})

如果进一步测试不起作用,我将删除此答案。