2 ms·
Dealing with sophisticated configurations of nested models and collections is definitely something that Backbone should be making easier than it is at the momen
by jashkenas 16y ago
Dealing with sophisticated configurations of nested models and collections is definitely something that Backbone should be making easier than it is at the moment. I think it would be a great feature to improve for the next release.
At the moment, the reason we've held off from any of the proposals, is that they all seem to tightly constrain your data structures and/or patterns of use. You can:
* Encode the model type into the JSON data, assuming that arrays map to collections, and nested objects map to models (not always the case).
* Employ some sort of mapping function that walks over the incoming JSON data (already possible with "parse").
And there are still a number of open questions:
* Does saving an inner model save everything in the outer model as well?
* How do nested model updates play with RESTful URLs?
* Can I refresh a portion of a nested model from the server-side?
* Do I have to adopt some sort of JSON-Path notation to be able to refer to specific attributes of nested models?
For these reasons, we've left nested models and collections as something you can choose to implement yourself, as best fits your app. If a good pattern emerges with consensus behind it, I'd love to roll it in to Backbone proper.
For the record, DocumentCloud uses a lot of nested collections on models that are lazy loaded and lazy rendered when the data is first requested. For example: displaying all of the annotations on a document, or a gallery of all the document's page images.