Note that there are some explanatory texts on larger screens.

plurals
  1. POStore and recover nested Backbone models into local storage
    text
    copied!<p>I'm looking for creating a complex backbone model architecture as :</p> <ul> <li>Backbone Model A <ul> <li>Backbone Model B1 (in A) <ul> <li>Collection Model C (in B1)</li> </ul></li> <li>Backbone Model B2 (in A)</li> </ul></li> </ul> <p>I wish to nest Backbone Models for the following reasons :</p> <ul> <li>I have a global view that will display information present in A and B</li> <li>I have a view that displays all B1 attributes</li> </ul> <p>In the end, I wanted one Backbone model for one view, but I must save a collection of A only into the local storage.</p> <p><strong>For now my principle is as follows :</strong></p> <p>When initializing A :</p> <ul> <li><p>In initalize() of A model I set :</p> <pre><code>this.attributes.modelB = new ModelB(); </code></pre></li> </ul> <p>For the moment, if I create an object modelA, I can access a B attribute as follows :</p> <pre><code>modelA.attributes.modelB.get ('id') // or modelA.attributes.modelB.attributes.id </code></pre> <p><em>In fact it's just a nested backbone model object</em></p> <p>Let's store A into local storage for a later use :</p> <ul> <li>I add A to my collection <em>“itemsCollection”</em></li> <li>I save A with <em>save()</em></li> </ul> <p>Let's take a look into the local storage :</p> <pre><code>localStorage.getItem ("itemsCollection") </code></pre> <p>Added A object is fully json parsed, as if by calling the method <em>save()</em>, each level of Backbone object into A model perform a <em>toJSON()</em>.</p> <p>All fields are well stored.</p> <p>Now I need to read this object to display the content in my view.</p> <ul> <li>I create a collection <em>"itemsCollection"</em> then I call method <em>fetch()</em>.</li> </ul> <p>Disaster, only the first level is recovered, over level are reset as empty B models.</p> <p>Something tells me the <em>fetch()</em> method wasn't made for recovering nested backbone model. But, my model pattern is simple and common, I need objects in objects, my solution may not be correct.</p> <p><strong>Do you have any suggestion for me ?</strong></p> <p>Thank you in advance</p>
 

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