问题
I would like to pass a json response to handlebars. I have looked at the parse documentation and stackoverflow questions but I cannot seem to figure this out.
This is the response:
{"results":[{"address":"755 W. Yale","createdAt":"2013-02-09T01:12:15.732Z","updatedAt":"2013-02-09T01:12:15.732Z","objectId":"JomKPfme5M"}]}
This is my handlebars template:
<script id="post-template" type="text/x-handlebars-template">
<h1>{{address}}</h1>
</script>
This is the script
Parse.initialize("xxxxxx", "yyyyyy");
var listingsView = Parse.Object.extend("listings");
var query = new Parse.Query(listingsView);
query.equalTo("objectId", "JomKPfme5M");
query.first({
success: function(results){
var source = $('#post-template').html();
var template = Handlebars.compile(source);
var html = template(results);
},
error: function(object, error){
console.log(error);
}
});
Thank you
回答1:
Results is an array. Try passing the first element to the template.
var html = template(results[0]);
回答2:
And if Hector's answer doesn't work, try this:
var html = template(results[0].attributes);
回答3:
If you are using EmberJS Ember-Model-Parse-Adapter is good
来源:https://stackoverflow.com/questions/14784907/display-json-response-from-parse-com-using-handlebars