1

I have a json object in collection which I need to show it on the page. Here is what I did: I first call the helpers template then in that I fetch the json object from the collection: I am using coffeescirpt and jade-handlebars, here goes my code in coffeescript:

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

In the console when I do Question.find().fetch() the following thing occurs:

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"

Now in the jade when I call the template by:

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

I can see only the [object] [object]. To see the question1,question2 I have to do the following:

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

How can I dynamically show all the questions without doing {{QuestionData.question1}} ...

Thank You in advance !!!

4 Answers 4

2

You can dynamically compose field names in a loop.

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

That being said, there's a lot here that seems a misunderstanding to me. I'd step back and say that you should look into storing one question per mongo document. This gives you the easiest data for meteor to play with. Or, storing multiple questions in an array:

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

The problems come when you think how you store the answers, sort the questions, etc. Probably a collection called questions, with a field maybe called sortOrder, a field called tag, etc.

How did you pick up calling templates this way, rather than having them as html files that a router manages for you?

Sign up to request clarification or add additional context in comments.

Comments

2

instead of just returning your json object with Questions.find().fetch() you could add another step to put your data into an array like:

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

return test;

(sorry for not writing in coffee script, i'm not aware of the language abstraction)

2 Comments

then how do we call it from the tempalte @RonH
in the console i still get Object {_id: "w9mGrv7LYNJpQyCgL", userid: "ntBgqed5MWDQWY4xt", QuestionData: Object, imgurl: "filepicker.io/api/file/e4jZ. I am still getting the "Object" when using QuestionData
2

To answer your question on how to do it, you can do something like this(in JS, sorry, not a 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;
};

And then in a template:

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

Something like that. But I agree with Jim Mack and that you should probably be storing this in an array.

1 Comment

ya i have gone with Jim Mack and figure it out myself. Thanks for all who have answer the question. you guys truly deserve the best. :) : )
0
+50

Like as JIm Mack Posted, save your collection in an array
first of all insert your question in an array by doing these in your coffeescript:

x = document.getElementById('question-form')
    length = x.length

    i = 0
    question = []
    while i< length
      aaa = x.elements[i]
      question.push
        questions: aaa
      i++

then since you are using Jade-handlebars you need register helpers in your jade file do these

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

        {{/each}}

The arrayify and myObject are the handlebars helpers. Then in your coffeescript

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

Hope these will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.