我第一次使用parse.com并使用javascript api搜索获取json响应的方法。实际上,我正在使用把手作为模板引擎并尝试从parse.com获取json响应,因此我可以轻松地将其传递给车把模板。目前,我得到的对象响应需要像result.get(“title”)那样做。我搜索了parse.com javascript指南和文档,但找不到这样做的方法。我想把parse.com类的所有feed记录作为json。有没有办法做任何这个?
这里的把手模板脚本::
<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>
示例json我用来测试模板::
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'
}
]
和parse.com脚本::
var feedsObject = Parse.Object.extend("feeds");
var query = new Parse.Query(feedsObject);
query.find({ ... });
我喜欢将其视为以下内容:
query.find({
success: function(feeds){
//load home page template
var source = $('#post-template').html();
var template = Handlebars.compile(source);
var html = template(feeds); // here's example with some details [link](http://screencast.com/t/XvPFuafRuIW)
$('#home .content').append(html);
},
error: function(object, error){
console.log(error);
}
});
提前致谢!!!
答案 0 :(得分:4)
我从未使用过Parse.com(实际上,并不知道它存在)。但是在示例here中,它返回一个Parse.Object
个实例的数组。
Parse.Object
有一个函数toJSON()。这应该做到。
所以在你的情况下:
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's example with some details [link](http://screencast.com/t/XvPFuafRuIW)
$('#home .content').append(html);
},
error: function(object, error){
console.log(error);
}
});