javascript - 为什么 meteor 不从我的模板助手中注入(inject)文本?

标签 javascript html mongodb meteor

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

模板/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;
        }   
    });
}

最佳答案

Jeremy 的回答其实更中肯,但是..

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

让我们更改 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() 
}

关于javascript - 为什么 meteor 不从我的模板助手中注入(inject)文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970033/

相关文章:

javascript - JS/JQuery : continue with the index of array of the form while generating new fields

javascript - UI5 需要文件,这些文件已捆绑到 Component-preload.js 中

HTML:非 float 自动宽度导航

javascript - OnClick JS If 语句

javascript - react Axios : How to send just the state value of my prop to backend

javascript - jQuery 平滑滚动,当页面滚动到相应部分时添加一个 active 类来锚定

javascript - WEB Audio API 滤波器 - 使用 slider 位置更改截止频率

html - MS WebBrowser + 嵌入式 HTML 资源 + res ://Protocol

node.js - MongoDB 查询将一个记录值与另一记录值相减

javascript - Mongo Meteor 中的排序跳过限制