scoretickr scoretickr - 5 months ago 14
JSON Question

Parse object with nested object parsing- Javascript

Thanks for looking into my question. I have two parse classes Game and CricketScore. Game has "first_inning_score" and "second_inning_score" pointers to CricketScore class.

I am only getting pointers to CricketScore as shown below. How can I get actual data from CricketScore?(RESOLVED). Now question is how to show data in handlebar??

I am getting this JSON Object from parse(updated)

{
"results": [
{
"first_inning_score": {
"overs": "0",
"parent": {
"__type": "Pointer",
"className": "Game",
"objectId": "Yibf5wHLIM"
},
"team_name": "Fr11",
"wickets": "0",
"innings": "1",
"runs": "0",
"createdAt": "2014-09-30T18:27:36.640Z",
"updatedAt": "2014-09-30T18:27:36.640Z",
"objectId": "0NhGUYySg5",
"__type": "Object",
"className": "CricketScore"
},
"second_inning_score": {
"wickets": "0",
"innings": "2",
"runs": "0",
"team_name": "Rb11",
"overs": "0",
"parent": {
"__type": "Pointer",
"className": "Game",
"objectId": "Yibf5wHLIM"
},
"createdAt": "2014-09-30T18:27:36.760Z",
"updatedAt": "2014-09-30T18:27:36.760Z",
"objectId": "4gHyHO84Ep",
"__type": "Object",
"className": "CricketScore"
},
"city": "Poway",
"elected": "bowl",
"groundname": "4s RANCH",
"hometeam": "Rb11",
"maxovers": "20",
"score_moderator": [
{
"__type": "Pointer",
"className": "_User",
"objectId": "00hBJbvgmu"
}
],
"sport": "Cricket",
"status": "inning1",
"team1": "Fr11",
"team2": "Rb11",
"toss": "Rb11",
"createdAt": "2014-09-30T18:27:36.504Z",
"updatedAt": "2014-09-30T18:27:36.898Z",
"objectId": "Yibf5wHLIM"
}
]
}


Handlebar code

<script type="text/x-handlebars-template" id="single-quote-template">
<!-- Feed Entry -->
<div class="row singleQuote">
<div class="panel" align="center">
<p><h4><strong> {{team1}} vs {{team2}}</strong></h4></p>
<p>Toss won by {{toss}} elected to {{elected}}, {{createdAt}}, {{groundname}}</p>
<p>{{first_inning_score.wickets}}</p> // ** <<---this is not working****

</div>
</div>
<hr />
</script>


Javascript code. I dont know how to get cricketscores yet. I am unable to follow Dehli's answer below.

query.find({
success: function(results) {
// The query was successful, and has passed
// back an array of PFObjects for you to use

// Since we're appending, clear the list out
// every time we're about to add results
$("#quoteList").html("");

// Compile the Handlebars template we're going
// to stick the results into. Pass Handlebars the
// ID of the script tags in index.html that contain
// the template.
var template = Handlebars.compile($("#single-quote-template").html());

// Iterate over all the results
$(results).each(function(i,e) {
// Serialize the PFObject and store it in q
var q = e.toJSON();
// Select the DOM element we're appending to,
// Then append the template, passing in q to
// provide the values of the template variables
$("#quoteList").append(template(q));
});
},
error: function(error) {
if (error.message == "unauthorized") {
// Temporary message if you haven't added your own credentials for Parse.com yet. Remove once set up.
console.warn("Please fill in your own Parse.com App ID and Javascript Key on line 107 of index.html");
}
// Handle the error here
}
})

Answer

What you need to do is add the following to your JavaScript query.

query.include("first_inning_score");
query.include("second_inning_score");
Comments