i'm using parse for the first time and searching for the way to get json response using javascript api. actually, i'm using handlebars as template engine and trying to get json response form parse so i can easily pass it to handlebars template. currently, i'm getting object response which needs to do like result.get("title"). i searched the parse javascript guide and documentation but couldn't find the way to do this. i'm want to get all the feeds records from parse class as json. is there any way to do this any soution??
here's handlebars 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 ment" >Comment</a>
<a href="javascript: alert('Share')" class="button share" >Share</a>
</div>
</div>
{{/each}}
</script>
sample json i'm using to test 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 script::
var feedsObject = Parse.Object.extend("feeds");
var query = new Parse.Query(feedsObject);
query.find({ ... });
i like to get it as following::
query.find({
success: function(feeds){
//load home page template
var source = $('#post-template').html();
var template = Handlebarspile(source);
var html = template(feeds); // here's example with some details [link]()
$('#home .content').append(html);
},
error: function(object, error){
console.log(error);
}
});
thanks in advance!!!
i'm using parse. for the first time and searching for the way to get json response using javascript api. actually, i'm using handlebars as template engine and trying to get json response form parse. so i can easily pass it to handlebars template. currently, i'm getting object response which needs to do like result.get("title"). i searched the parse. javascript guide and documentation but couldn't find the way to do this. i'm want to get all the feeds records from parse. class as json. is there any way to do this any soution??
here's handlebars 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 ment" >Comment</a>
<a href="javascript: alert('Share')" class="button share" >Share</a>
</div>
</div>
{{/each}}
</script>
sample json i'm using to test 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. script::
var feedsObject = Parse.Object.extend("feeds");
var query = new Parse.Query(feedsObject);
query.find({ ... });
i like to get it as following::
query.find({
success: function(feeds){
//load home page template
var source = $('#post-template').html();
var template = Handlebars.pile(source);
var html = template(feeds); // here's example with some details [link](http://screencast./t/XvPFuafRuIW)
$('#home .content').append(html);
},
error: function(object, error){
console.log(error);
}
});
thanks in advance!!!
Share Improve this question edited Jan 2, 2013 at 11:42 krozero asked Jan 2, 2013 at 10:58 krozerokrozero 6,4933 gold badges22 silver badges34 bronze badges 3- @asgoth here's handlebars template script: link and the demo json object using with it: link. now i'm trying to get same json response from parse.. and here is my parse script: link (i know the script is wrong but it's example how i want the response to be (as a json so i can pass the as it is like in screenshot)). currently, there is multiple datas and i have to do query.count and something like feeds[i].get("feed_des") etc. – krozero Commented Jan 2, 2013 at 11:30
- it might be easier if you just copy/paste the snippets in your question. Makes it easier for SO users to follow. – asgoth Commented Jan 2, 2013 at 11:32
- @asgoth - question updated with snippets. – krozero Commented Jan 2, 2013 at 14:56
1 Answer
Reset to default 4I've never used Parse. (in fact, didnt know it existed). But in the example here, it returns an array of Parse.Object
instances.
Parse.Object
has a function toJSON(). This 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.pile(source);
var html = template(jsonArray); // here's example with some details [link](http://screencast./t/XvPFuafRuIW)
$('#home .content').append(html);
},
error: function(object, error){
console.log(error);
}
});