Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping JSON to backbone.js collections
    primarykey
    data
    text
    <p>Alright, it looks like I need a hint to point me in the right direction. This question is two part - working with mult-dimensional JSON and Collections of Collections from JSON.</p> <h2>Background</h2> <p>I have some JSON that is going to be retrieved from a server and have control over how it could be formatted.</p> <h2>Multi-Dimentional JSON</h2> <p>I'm having some trouble being able connecting the model to the parts in the JSON. Say I wanted to render just each of the posts <em>author name</em>, and the content of <em>status</em> in the sample JSON below. I'm having no problem getting the status into the model, but the author name I'm a bit confused how to get to it. From my understanding I have to override the parse.</p> <p>Is this bad standards / is there a better JSON structure I should use? Would it be better to keep it as flat as possible? That is move the author name and photo up one level?</p> <p>I was reading <a href="https://stackoverflow.com/questions/8782619/how-to-build-a-collection-model-from-nested-json-with-backbone-js">How to build a Collection/Model from nested JSON with Backbone.js</a> but it is still a little unclear to me.</p> <h2>Collection in Collections</h2> <p>Is there a nice way to make a collection within a collection for backbone.js? I will have a collection of posts, and then would have a collection of comments on that post. As I'm developing in backbone is that even possible?</p> <p>From what I understand in <a href="https://stackoverflow.com/questions/10388199/backbone-js-collection-of-collections">Backbone.js Collection of Collections</a> and <a href="https://stackoverflow.com/questions/13675539/backbone-js-collection-of-collections-issue">Backbone.js Collection of Collections Issue</a>, it would look something like this?</p> <pre><code>var Comments = Backbone.Model.extend({ defaults : { _id : "", text : "", author : "" } }) var CommentsCollection = Backbone.Collection.extend({ model : Comments }) var Posts = Backbone.Model.extend({ defaults : { _id : "", author : "", status : "", comments : new CommentsCollection } }) var PostsCollection = Backbone.Collection.extend({ model : Posts }) </code></pre> <h3>Sample JSON</h3> <pre><code>{ "posts" : [ { "_id": "50f5f5d4014e045f000002", "author": { "name" : "Chris Crawford", "photo" : "http://example.com/photo.jpg" }, "status": "This is a sample message.", "comments": [ { "_id": "5160eacbe4b020ec56a46844", "text": "This is the content of the comment.", "author": "Bob Hope" }, { "_id": "5160eacbe4b020ec56a46845", "text": "This is the content of the comment.", "author": "Bob Hope" }, { ... } ] }, { "_id": "50f5f5d4014e045f000003", "author": { "name" : "Chris Crawford", "photo" : "http://example.com/photo.jpg" }, "status": "This is another sample message.", "comments": [ { "_id": "5160eacbe4b020ec56a46846", "text": "This is the content of the comment.", "author": "Bob Hope" }, { "_id": "5160eacbe4b020ec56a46847", "text": "This is the content of the comment.", "author": "Bob Hope" }, { ... } ] }, { ... } ]} </code></pre> <p>I appreciate even any hints to guild me. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload