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