在集合中导航数组和在流星中嵌套模板

时间:2014-09-17 10:07:56

标签: meteor

我正在玩meteor并尝试使用相同的方法创建一个简单的测验应用程序。我以这种方式设置我的问题集:

{question: "Why did the chicken cross the road??", choices: ["To eat", "To die", "It depends", "There is no chicken"], correctAnswer:2, number : 1}, 
{question: "Who was the first man to step on moon's surface?", choices: ["Yo Yo Honey Singh", "Neil Armstrong", "Buzz Eldrin", "Rakesh Sharma"], correctAnswer:1, number : 2},       
{question: "Where is Timbuktu? ", choices: ["Asia", "There is no such place", "Africa", "Europe"], correctAnswer:2, number : 3}, 
{question: "Who said 'there is no pill'?", choices: ["Morpheus", "Mr. Anderson", "Neo", "Yoda"], correctAnswer:0, number : 4}, 
{question: "What is the one thing that saved Arthur Dent's life multiple times?", choices: ["The bugblatter beast of Tral", "Ford Perfect", "A towel", "The babel fish"], correctAnswer:2 , number : 5}

我能够从集合中获得问题但是却在努力获取每个问题的选项(如上所示存储在数组中)。我创建了两个模板,一个用于问题词干,另一个用于选择。选项模板嵌套在问题模板中,如下所示:

<template name="question">
  <p>{{ currentQuestion.question }}</p>
    <form>
      {{ #each currentQuestion.choices }}
      <div class="radio answers">
        {{> choices}}
     </div>
     {{ /each }}
    </form>
  </p>
</template>
<template name="choices">
    <input type="radio" name="answer" id="{{ choice }}"><label for="{{ choice }}">{{ choice }}</label><br>
</template>

显然这不起作用。现在我的问题是:

  • 这是设置我的问题集的最佳方式吗?
  • 如何设置嵌套模板以导航和显示我的集合中的选项数组?

提前致谢。期待为此提供一些解决方案。

干杯..

1 个答案:

答案 0 :(得分:1)

<template name="questions">
   {{#each questions}}
      {{> question}}
   {{/each}}
</template>

<template name="question">
    <p>{{ question }}</p>
    <form>
      {{ #each choices }}
        <div class="radio answers">
          {{> choices}}
        </div>
      {{ /each }}
    </form>
</template>

<template name="choices">
    <input type="radio" name="answer" id="{{ this }}"><label for="{{ this }}">{{ this }}</label><br>
</template>

See working sample in meteorpad