2016-03-08 104 views
2

我刚接触Javascript和流星 - 我敢肯定我犯了一个非常基本的错误。开始使用流星 - 显示集合

我想了解如何显示集合的内容。

这是基于对流星网站上的教程 - 我已经替换为“任务”“SW”这也是我收藏的名称

Template.HomePrivate.helpers({ 

}); 

sw = new Mongo.Collection("sw"); 

if (Meteor.isClient) { 
    // This code only runs on the client 
    Template.body.helpers({ 
    text: function() { 
     return sw.find({}); 
    } 
    }); 
} 

HTML:

<template name="HomePrivate"> 
    <div class="page-container container" id="content"> 
     <div class="row" id="title_row"> 
      <div class="col-md-12"> 
       <h2 id="page_title" class="pull-left"> 
        Welcome {{userFullName}}! 
       </h2> 
       <div id="page_menu" class="pull-right"> 
       </div> 
      </div> 
     </div> 
    </div> 
    <div class="container"> 
    <header> 
     <h1>Sight Words</h1> 
    </header> 
<p>begin list</p> 
    <ul> 
     {{#each sw}} 
     {{> sw}} 
     {{/each}} 
    </ul> 
    <p>end list</p> 
    </div> 
</template> 

<template name="sw"> 
    <li>{{text}}</li> 
</template> 

回答

0

你的#each循环没有显示任何内容,因为sw没有被定义为助手:

{{#each sw}} 
    {{> sw}} 
{{/each}} 

Blaze寻找助手名字时使用你的#each循环 - 我认为这是你的困惑所在。在这种情况下,你想用你上面定义的帮手,text

{{#each text}} 
    {{> sw}} 
{{/each}} 

sw尚未被定义为一个帮手,让火焰是不承认它。

现在,{{> sw}}是有效的语法,因为它被定义为模板(这是>符号的用途)。假设你的sw集合有一个字段text,你的#each循环现在应该正确显示。