为什么流星不从我的模板助手中注入文本

Why isn't meteor injecting the text from my template helpers?

本文关键字:注入 文本 流星 我的 为什么      更新时间:2023-09-26

>我试图动态生成包含两组不同数据的表。我的数据库不是空的,退货也已经过验证。但是当我检查呈现的页面时,相应的 HTML 不存在,好像没有返回任何内容。

模板/网页:

<template name="room">
<div class="container-fluid">
<h1> Sprint Retrospective</h1>
<hr>
<div class="input-group">
<input type="text" class="form-control thoughts" placeholder="Thoughts..." aria-describedby="basic-addon1">
<span class="input-group-addon">
        <input id="wentWell" type="checkbox" aria-label="..."> Went Well
      </span>
<span class="input-group-addon">
        <input id="wentWrong" type="checkbox" aria-label="..."> Went Wrong
      </span>
 <span class="input-group-btn">
        <button class="btn btn-default" type="button">Submit!</button>
      </span>
</div>
<hr>
{{#if haveCards}}
    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-6 col-sm-6">
                <div class="row">Went Well</div>
                {{#each wentWell}}
                    {{>card}}
                {{/each}}
            </div>
            <div class="col-xs-6 col-sm-6">
                <div class="row">Went Wrong</div>
                {{#each wentWrong}}
                    {{>card}}
                {{/each}}
            </div>
        </div>
    </div>
{{/if}}
</div>
</template>

Javascript:

"use strict";
/**
*
**/
var Cards = new Mongo.Collection('cards');
var allCards;
var wentWellCards;
var wentWrongCards;
if(Meteor.isClient){
    Tracker.autorun(function(){
         allCards = Cards.find({},{sort:{createdAt:-1}});
         wentWellCards = Cards.find({category:"Went Well"},{sort:{createdAt:-1}});
         wentWrongCards = Cards.find({category:"Went Wrong"},{sort:{createdAt:-1}});
    });
    Template.room.helpers({
        haveCards: function(){
            if(allCards != null && allCards != undefined && allCards.length > 0)
                return true;
            return false;
        },
        wentWell: function(){
            return this.wentWellCards;
        },
        wentWrong: function(){
            return this.wentWrongCards;
        }   
    });
}

>杰里米的回答实际上更有意义,但是..

让我们尝试稍微修复一下该代码。

让我们更改wentWellwentWrong助手,使其看起来像这样更干净。

    wentWell: function(){
        return Cards.find({category:"Went Well"},{sort:{createdAt:-1}});
    },
    wentWrong: function(){
        return Cards.find({category:"Went Wrong"},{sort:{createdAt:-1}});
    }  

同样对于haveCards助手,您可以执行以下操作

haveCards: function(){
 return Cards.find().count() >= 1 //for example or return just findOne() 
}

你的助手应该返回 wentWellCards 而不是 this.wentWellCards 等。

您的帮助程序不是反应式的,因此,当加载数据时(在呈现页面后发生),帮助程序不会重新运行。

简单地说,直接在帮助程序中调用反应式方法(minimongo 查询)。一旦数据可用,这将使它们重新运行

此外,当您检查计数时,您需要获取集合

Cards = new Mongo.Collection('cards');
if(Meteor.isServer){
    Meteor.publish('cards', function() {
        return Cards.find({},{sort:{createdAt:-1}});
    });
}
if(Meteor.isClient){
    Template.room.onCreated(function(){
        this.subscribe('cards');
    });
    Template.room.helpers({
        haveCards: function(){
            var allCards = Cards.find({},{sort:{createdAt:-1}}).fetch();
            return (allCards != null && allCards != undefined && allCards.length > 0);
        },
        wentWell: function(){
            return wentWellCards = Cards.find({category:"Went Well"},{sort:{createdAt:-1}});
        },
        wentWrong: function(){
            return wentWrongCards = Cards.find({category:"Went Wrong"},{sort:{createdAt:-1}});
        }   
    });
}

并且您需要从服务器发布集合并从模板订阅(除非您使用自动发布)