using JSON in Rails, Backbone and Mustache - formats seem to differ -
i trying put rails app, backbone.js , mustache templating. finding json required backbone isn't compatible json required mustache. (i started out following tutorial cloudedit -a backbone.js tutorial example, want use mustache using jst.
for backbone must set activerecord::base.include_root_in_json = false. model (person firstname , surname) data sent rails /people looks this:
[{"firstname":"jane","surname":"jones"},{"firstname":"janet","surname":"jensen"}]
my mustache template looks this:
<h3><a href='#new'>create new</a></h3> <h4>people</h4> <ul> {{#people}} <li>{{firstname}} {{surname}}</li> {{/people}} </ul>
and mustache docs expect wants see is
{"people":[{"firstname":"jane","surname":"jones"},{"firstname":"janet","surname":"jensen"}]}
edited out me transforming js collection json , sending mustache. don't need that. mustache.js expects see js objects, not strings of json.
by time mustache.to_html have backbone collection of models. models have attributes firstname , surname. looks in firebug:
collection +_bycid +_byid length 2 - models [object { attributes=(...), more...}, object {attributes=(...), more...}] - 0 object { attributes=(...), more...} ....... more properties of object + attributes object {firstname="janet", surname="jensen"}
there seem couple of problems here. 1. there no mention of name of collection (people). can around in various ways guess, @ least using {{#models}}..{{/models}} in template.
- the attributes buried deeper mustache.js looks. when gets down trying find 'firstname' tag in object, looks object['firstname'] , doesn't find it, object.attributes['firstname'] has correct value.
it seems i'm mixed here....so doing wrong? , how can fix it?
i have solution of sort (thanks this question).
jsonfortemplate = json.parse(json.stringify(this.people)); //this works single item context['people']=jsonfortemplate; //need add collection out = mustache.to_html(tpl,context);
but feels though sort of jumping-through-hoops shouldn't necessary. check see if handlebars offers better.
Comments
Post a Comment