Using JSON in Rails, Backbone, and Mustache - the formats seem to be different

I am trying to build a Rails application with backbone.js templates and a mustache. I find that the JSON required by the backbone is incompatible with the JSON required by Usach. (I started with this tutorial, the Cloudedit -a backbone.js tutorial is an example , but I want to use Mustache, where it uses JST.

For the trunk, we must set ActiveRecord :: Base.include_root_in_json = false. For my model (a person with a first and last name), the data sent by the rails from / people looks like this:

[{"firstname":"Jane","surname":"Jones"},{"firstname":"Janet","surname":"Jensen"}]

My mustache pattern is as follows:

<h3><a href='#new'>Create New</a></h3>
<h4>People</h4>
<ul>
{{#people}}
<li>{{firstname}} {{surname}}</li>
{{/people}}
</ul>

and from the documents of the mustache I expect that what he wants to see

{"people":[{"firstname":"Jane","surname":"Jones"},{"firstname":"Janet","surname":"Jensen"}]}

, JS JSON Mustache. . Mustache.js js-, JSON.

Mustache.to_html . firstname surname. firebug:

collection
  +_byCid
  +_byId
   length 2
  - models  [object { attributes=(...), more...}, object {attributes=(...), more...}]
    - 0 object { attributes=(...), more...}
      ....... some more properties of the object
        + attributes object {firstname="Janet", surname="Jensen"}

, . 1. (). , -, , {{#models}}.. {{/models}} .

  • , Mustache.js. "firstname" , ['firstname'] , object.attributes ['firstname'] .

, ... ? ?

+5
2

( ).

jsonForTemplate = JSON.parse(JSON.stringify(this.people)); //this works for a single item
context['people']=jsonForTemplate;    //need to add this for a collection
out = Mustache.to_html(tpl,context);

, . , , - .

+2

toJSON , - http://documentcloud.github.com/backbone/#Collection-toJSON

out = Mustache.to_html(tpl, collection.toJSON);
+2

All Articles