Handlebar无法正确呈现集合字段

时间:2013-08-07 14:09:17

标签: collections meteor render handlebars.js

我在尝试在模板中使用名为created的集合字段时遇到问题。这是一个保留的词,还是什么?

正在挣扎的模板部分看起来像这样:

{{#each threads}}
    <tr>
        <td><a href="forumShowThread?id={{_id}}">{{topic}}</a></td>
        <td>{{creator.username}}</td>
        <!-- The line below is the evil one. -->
        <td>{{created}}</td>
        <td>{{lastPost.poster.username}} {{datetime lastPost.posted}}</td>
    </tr>
{{/each}}

我在浏览器的控制台中找到的线程如下:

[
Object
_id: "ngEtonq8XM36KtG3S"
created: 1375881336372
creator: function (){
creatorId: "ZmKpMdhP4GtzQo98e"
lastPost: function (){
posts: function (){
subCategory: function (){
subCategoryId: "axgd2xzctkfmphmwM"
topic: "Testing"
__proto__: Object
, 
Object
_id: "XafEMvAvcRzpBKxG3"
created: 1375882602652
creator: function (){
creatorId: "ZmKpMdhP4GtzQo98e"
lastPost: function (){
posts: function (){
subCategory: function (){
subCategoryId: "axgd2xzctkfmphmwM"
topic: "Testnign again"
__proto__: Object
, 
Object
_id: "CZmf5MfqZrB28SLPB"
created: 1375883440242
creator: function (){
creatorId: "ZmKpMdhP4GtzQo98e"
lastPost: function (){
posts: function (){
subCategory: function (){
subCategoryId: "axgd2xzctkfmphmwM"
topic: "And another shoot"
__proto__: Object
]

显然有三个线程,它们都有一个已创建的字段。但浏览器显示以下内容:

<tr>
    <td><a href="forumShowThread?id=CZmf5MfqZrB28SLPB">And another shoot</a></td>
    <td>Peppe L-G</td>
    <td></td>
    <td>Peppe L-G 7 August 2013 15:50</td>
</tr>

<tr>
    <td><a href="forumShowThread?id=XafEMvAvcRzpBKxG3">Testnign again</a></td>
    <td>Peppe L-G</td>
    <td></td>
    <td>Peppe L-G 7 August 2013 15:36</td>
</tr>

<tr>
    <td><a href="forumShowThread?id=ngEtonq8XM36KtG3S">Testing</a></td>
    <td>Peppe L-G</td>
    <td></td>
    <td>Peppe L-G 7 August 2013 15:35</td>
</tr>

为什么created字段没有显示?

我尝试使用<td>{{this.created}}</td>代替<td>{{created}}</td>,然后浏览器显示以下内容:

<tr>
    <td><a href="forumShowThread?id=CZmf5MfqZrB28SLPB">And another shoot</a></td>
    <td>Peppe L-G</td>
    <td>1375883440242</td>
    <td>Peppe L-G 7 August 2013 15:50</td>
</tr>

<tr>
    <td><a href="forumShowThread?id=XafEMvAvcRzpBKxG3">Testnign again</a></td>
    <td>Peppe L-G</td>
    <td></td>
    <td>Peppe L-G 7 August 2013 15:36</td>
</tr>

<tr>
    <td><a href="forumShowThread?id=ngEtonq8XM36KtG3S">Testing</a></td>
    <td>Peppe L-G</td>
    <td></td>
    <td>Peppe L-G 7 August 2013 15:35</td>
</tr>

created字段现在适用于第一个线程,但不适用于其余线程。发生什么事了?!

如果它是相关的,这是整个模板:

<template name="pageForumSubCategory">
    <div class="layoutContentForumSubCategory">
        {{#with subCategory}}
            <h1>
                <a href="forum">Forum</a>
                →
                {{name}}
                {{#if currentUser}}
                    →
                    <a href="forumCreateNewThread?id={{_id}}">New thread</a>
                {{/if}}
            </h1>
            {{#if threads.count}}
                <table border="2">
                    <thead>
                        <tr>
                            <th>Topic</th>
                            <th>Creator</th>
                            <th>Created</th>
                            <th>Last post</th>
                        </tr>
                    </thead>
                    <tbody>
                        {{#each threads}}
                            <tr>
                                <td><a href="forumShowThread?id={{_id}}">{{topic}}</a></td>
                                <td>{{creator.username}}</td>
                                <!-- The line below is the eveil one. -->
                                <td>{{this.created}}</td>
                                <td>{{lastPost.poster.username}} {{datetime lastPost.posted}}</td>
                            </tr>
                        {{/each}}
                    </tbody>
                </table>
            {{else}}
                <p>Well, it's more or less empty here (so far).</p>
            {{/if}}
        {{else}}
            <h1>
                <a href="forum">Forum</a>
                →
                ???
            </h1>
            <p>Hmm... There is no subforum with the given id. Strange, but there's nothing I can do about it. Sorry.</p>
        {{/with}}
    </div>
</template>

1 个答案:

答案 0 :(得分:0)

所有模板都有created属性,即回调(在创建模板时运行)。最简单的解决方案就是将字段称为createdAt,而不是{{1}}。