How to get json parse.com response form using query.find (); using javascript in a web application?

I am using parse.com for the first time and am looking for a way to get json response using javascript api. in fact, I use handlebars as a template engine and try to get the json parse.com response form, so I can easily pass it to the handlebars template. I am currently receiving a response from an object that should do as result.get ("title"). I searched the parse.com javascript manual and documentation, but couldn't find a way to do this. I want to get all feed entries from parse.com class as json. is there any way to do this any soution ??

here's the handle template script ::

<script id="post-template" type="text/x-handlebars-template"> {{#each feeds}} <div class="post" style="padding-top: 5px; width: 307px; margin: 0 auto;" > <div style="background: rgba(255,255,255, 0.5); text-align: left;"> <img src="{{post-img}}" alt="image"/> <p>{{post-mes}}</p> <p style="position: relative;"> <img src="{{post-by-img}}" alt="image" /><label> By @{{post-by}}</label> <label class="ago" style="vertical-align: top; position: absolute; right: 10px; top: 18px; height: 20px;">{{post-dt}}</label> </p> </div> <div> <a href="javascript: alert('Like')" class="button like" >Like</a> <a href="javascript: alert('Comment')" class="button comment" >Comment</a> <a href="javascript: alert('Share')" class="button share" >Share</a> </div> </div> {{/each}} </script> 

sample json I use to test the template ::

 feeds:[ { 'post-img': 'assets/images/photo1.png', 'post-mes': 'Before the party, with @Marie', 'post-by': 'Naza', 'post-by-img':'assets/images/by.jpg', 'post-dt': '5 min' }, { 'post-img': 'assets/images/photo1.png', 'post-mes': 'Before the party, with @Marie', 'post-by': 'Naza', 'post-by-img':'assets/images/by.jpg', 'post-dt': '10 min' }, { 'post-img': 'assets/images/photo1.png', 'post-mes': 'Before the party, with @Marie', 'post-by': 'Naza', 'post-by-img':'assets/images/by.jpg', 'post-dt': '15 min' } ] 

and parse.com script ::

 var feedsObject = Parse.Object.extend("feeds"); var query = new Parse.Query(feedsObject); query.find({ ... }); 

I like getting it like the following:

 query.find({ success: function(feeds){ //load home page template var source = $('#post-template').html(); var template = Handlebars.compile(source); var html = template(feeds); // here example with some details [link](http://screencast.com/t/XvPFuafRuIW) $('#home .content').append(html); }, error: function(object, error){ console.log(error); } }); 

thanks in advance!

+4
source share
1 answer

I never used Parse.com (in fact, I did not know that it exists). But in the example here , it returns an array of Parse.Object instances.

Parse.Object has a toJSON () function. That should do it.

So in your case:

 query.find({ success: function(feeds){ var jsonArray = []; for(var i = 0; i < feeds.length; i++) { jsonArray.push(feeds[i].toJSON()); } //load home page template var source = $('#post-template').html(); var template = Handlebars.compile(source); var html = template(jsonArray); // here example with some details [link](http://screencast.com/t/XvPFuafRuIW) $('#home .content').append(html); }, error: function(object, error){ console.log(error); } }); 
+4
source

All Articles