Backbone nested models is meant to be a lightweight and simple solution for nested models in Backbone.js. It gives you nothing fancy, just simple relations (2.1 kilobytes minified). If you are looking for a larger more complex and exhaustive solution checkout Backbone-relational by Paul Uithol.
Backbone.Model can contain an array of
relations. Each Relation defines a nested model or collection which will
be represented by that attribute.
var Book = BackboneModel;var book = ;book;var author = book;author === "Heber J. Grant";authorparent === 450;
Whenever set is called on a model, or a json response is saved to a model, if a relation exists for one of the attributes
being saved, a new instance of that relation will be created or if it already exists, it will be updated. Also note that
the relation will be given a backwards reference to its parent model through the keyword
_parent attribute will also be added to the options paramter passed to the initialize method, but be aware that the parent model is not entirely initialized at this point.
Collections work similarly except that the backwards reference from a model which is part of a colleciton which is a
relation of a model is
var Package = BackboneModel;var pkg = ;pkg;var document1 = pkg;document1 === "Document 1";document1collectionparent === "My Package";
Backbone nested models will never recreate an entire relation as an attribute to a model. It only creates the relation once and thereafter updates the collection or model representing the relation. This is important because event bindings or cached references may be pointing to the previous relations, and if they are recreated those references would be lost.
- Explicitly depend upon underscore - https://github.com/blittle/backbone-nested-models/pull/25
- Use build int
- Add AMD support
- Fix toJSON from failing when relations are defined but don't exist
- Feature - Call reset event for nested Models/Collections - #5
- Feature - Recursively call .toJSON()
- Fix memory leak issue where parent references weren't removed on "unset"
- Fix various issues with the collection merging.
- Add karma test suite
(The MIT License)
Copyright (c) 2012 Bret Little
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.