1

コレクションに json オブジェクトがあり、ページに表示する必要があります。これが私がやったことhelpers templateです: コレクションから json オブジェクトを取得するという点で、最初に then を呼び出します: 私は coffeescirpt と jade-handlebars を使用しています。

Template.test.helpers
  test: ->
    test = Question.find().fetch(); 
    test

コンソールで次のことを行うとQuestion.find().fetch()、次のことが発生します。

QuestionData: Object
question1: "How many kids do I have ?"
question2: "when will i die ?"
question3: "how many wife do i have ?"
question4: "test"
__proto__: Object
_id: "w9mGrv7LYNJpQyCgL"
userid: "ntBgqed5MWDQWY4xt"
specialNote: "Rohan ale"

次の方法でテンプレートを呼び出すと、玉の中にあります。

template(name="hello")
  .test {{QuestionData}}

しか見えない[object] [object]。question1,question2 を見るには、次のことを行う必要があります。

template(name="hello")
  .test {{QuestionData.question1}}, {{QuestionData.question2}}

何もせずにすべての質問を動的に表示するにはどうすればよいですか{{QuestionData.question1}}...

前もって感謝します !!!

4

4 に答える 4

2

それを行う方法についての質問に答えるには、次のようにすることができます (JS では、申し訳ありませんが、coffeeScripter ではありません)。

Template.Questionnaire.questions = function () {
    var questions = [];
    _.each(Object.keys(this), function (field) {
        if(/^question.+/.test(field)) {
            questions.push({ label: field, question: this[field]});
        }            
    });
    return questions;
};

そして、テンプレートで:

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

そんな感じ。しかし、私はジム・マックに同意し、おそらくこれを配列に格納する必要があります。

于 2013-09-24T15:24:55.003 に答える