【问题标题】:Display json object dynamically动态显示json对象
【发布时间】:2013-09-13 21:40:34
【问题描述】:

我在集合中有一个 json 对象,我需要在页面上显示它。 这是我所做的: 我首先调用 helpers template 然后从集合中获取 json 对象: 我正在使用 coffeescirpt 和玉手把,这是我在 coffeescript 中的代码:

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]。要查看问题 1、问题 2,我必须执行以下操作:

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

如何在不做{{QuestionData.question1}}的情况下动态显示所有问题...

提前谢谢你!!!

【问题讨论】:

    标签: javascript json coffeescript meteor


    【解决方案1】:

    您可以在循环中动态组合字段名称。

    b = { q1: 'a1', q2: 'a2', q3: 'a3' };
    for (x=1;x<=3;x++) { console.log( b[ 'q' + x ] ) }
    

    话虽如此,这里有很多地方对我来说似乎是一个误解。我会退后一步说您应该考虑为每个 mongo 文档存储一个问题。这为您提供了最简单的流星数据。或者,将多个问题存储在一个数组中:

    test = {
      questions : [ 
        "How many kids do I have ?"
        "when will i die ?"
        "how many wife do i have ?"
        "test" ] ,
    userid: "ntBgqed5MWDQWY4xt",
    specialNote: "Rohan ale"
    }
    

    当您考虑如何存储答案、对问题进行排序等时,问题就来了。可能是一个名为 questions 的集合,其中一个字段可能称为 sortOrder,一个字段称为 tag 等。

    您是如何以这种方式获取调用模板的,而不是将它们作为路由器为您管理的 html 文件?

    【讨论】:

      【解决方案2】:

      除了使用 Questions.find().fetch() 返回您的 json 对象外,您还可以添加另一个步骤将数据放入数组中,例如:

      test = function() { 
        var temp = [];
        for (item in Questions.find().fetch()) {
          temp.push(item);
        };
        return temp;
      };
      
      return test;
      

      (抱歉没有用咖啡脚本写,我不知道语言抽象)

      【讨论】:

      • 那么我们如何从模板中调用它@RonH
      • 在控制台中我仍然得到 Object {_id: "w9mGrv7LYNJpQyCgL", userid: "ntBgqed5MWDQWY4xt", QuestionData: Object, imgurl: "filepicker.io/api/file/e4jZ。使用 QuestionData 时我仍然得到 "Object"
      【解决方案3】:

      要回答您关于如何操作的问题,您可以执行以下操作(在 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>
      

      类似的东西。但我同意 Jim Mack 的观点,您可能应该将它存储在一个数组中。

      【讨论】:

      • 是的,我已经和 Jim Mack 一起去解决了。感谢所有回答问题的人。你们真的应该得到最好的。 :) :)
      【解决方案4】:

      就像 JIm Mack 发布的那样,将您的收藏保存在一个数组中
      首先,通过在您的咖啡脚本中执行以下操作将您的问题插入到数组中:

      x = document.getElementById('question-form')
          length = x.length
      
          i = 0
          question = []
          while i< length
            aaa = x.elements[i]
            question.push
              questions: aaa
            i++
      

      那么由于您使用的是 Jade-handlebars,您需要注册助手 在你的玉文件中做这些

       {{#each arrayify myObject}}
                        {{#each this.name}}
                          p {{questions}}
                        {{/each}}
      
              {{/each}}
      

      arrayifymyObject 是车把助手。然后在你的咖啡脚本中

      Handlebars.registerHelper "arrayify", (obj) ->
        result = []
        for temp in obj
          userQuestion = temp.question
          result.push
            name: userQuestion
        return result
      
      Template.templatename.myObject = ->
        temp = []
        for item in Question.find().fetch()
          temp.push item
        return temp
      

      希望这些能奏效。

      【讨论】:

        猜你喜欢
        • 2021-01-30
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        • 2012-11-20
        • 2013-04-16
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        相关资源
        最近更新 更多