Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Backbone.js support for nested collection/models is non-existent, and they provide no saving support (see <a href="http://documentcloud.github.com/backbone/#FAQ-nested" rel="nofollow">http://documentcloud.github.com/backbone/#FAQ-nested</a>). You have to override toJSON on any model with a subcollection. I've run into this scenario a million times. If you have something like (in coffeescript):</p> <pre><code>class MainModel extends Backbone.Model itemCollection: -&gt; @_itemCollection ?= new ItemCollection(@get('itemCollection')) class ItemCollection extends Backbone.Collection model: ItemModel class ItemModel extends Backbone.Model subCollection: -&gt; @_subCollection ?= new SubCollection(@get('subCollection')) class SubCollection extends Backbone.Collection model: SubModel class SubModel extends Backbone.Model mainModel = new MainModel(json) </code></pre> <p>Then in order for mainModel.save() to work, you need to override toJSON on MainModel and ItemModel, like:</p> <pre><code>class MainModel extends Backbone.Model itemCollection: -&gt; @_itemCollection ?= new ItemCollection(@get('itemCollection')) toJSON: -&gt; return _.extend(@attributes, {itemCollection: @itemCollection().toJSON()}) class ItemModel extends Backbone.Model subCollection: -&gt; @_subCollection ?= new SubCollection(@get('subCollection')) toJSON: -&gt; return _.extend(@attributes, {subCollection: @subCollection().toJSON()}) </code></pre> <p>I wrote the example in coffeescript because it's much more concise than javascript. If you need any help making sense of it, please just ask.</p> <p>Hope this helps!</p> <p>--- Note ---</p> <p>Technically, in coffeescript, the toJSON methods could simply be:</p> <pre><code>toJSON: -&gt; _.extend @attributes, itemCollection: @itemCollection().toJSON() </code></pre> <p>But I wrote it the way I did to be more understandable to non-coffeescripters.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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