A plugin to create entire Document structures with nested Backbone.js Models & Collections with deep model
references and event bubbling
.
The Document is simply a reference to the project's goal of allowing MongoDB Document JSON representation to be dynamically composed/referenced/updated and saved using native Backbone.js components.
Another Backbone.js plugin...
After working with document objects we kept running into a situation where we wanted to pass Model/Collection objects to our nested Backbone.Views, however this proved troublesome to keep track of changes made within those Views.
// Setup our Document Model object.user;
When making a new Backbone.View its common to pass in a Model or Collection, and it would be best practice to pass only the specific Model/Collection that the control needed.
var AddressModalView = BackboneView; var UserView = BackboneView;
Usage
-
Download the latest version here, and add
backbone-documentmodel.js
to your HTML<head>
, afterbackbone.js
is included. -
Change your models to extend from
Backbone.DocumentModel
, e.g.var Person = BackboneModel;// becomesvar Person = BackboneDocumentModel; -
Change your collections to extend from
Backbone.DocumentCollection
, e.g.var People = BackboneCollection;// becomesvar People = BackboneDocumentCollection;
Nested Attributes & Document Composition
get()
and set()
will work as before, you can now reference deep model names and set()
will also dynamically compose Document data into nested Models & Collections:
1-1
set()
// object syntax - will generate entire Model/Collection nested objects.// NOTE: be careful not to overwrite existing Collections as a set Array// will overwrite an existing Collection.user; user; // dot syntax - will create new Models and properties (not Collections).// NOTE: dynamic Collection composition is not supported on array indicies,// however you can update an existing array item.// ex: 'addresses.0.city': 'Charlottesville'user; user; // dynamic composition of Backbone Model [M], Collection [C] and Attribute [A]// user [M]// - name [M]// - first [A]// - last [A]// - middle [M]// - initial [A]
get()
// dot syntaxuser; // returns 'John'user; // returns 'Z'user; // returns 'Z' // directuser; // returns 'John'user; // returns 'Z'
1-N
set()
// object syntax - will generate entire Model/Collection nested objects.// NOTE: be careful not to overwrite existing Collections as a set Array// will overwrite an existing Collection.user; user;user; // dot syntax - will update existing Collection items, non-existing items are ignored.// NOTE: dynamic Collection composition is not supported on array indicies,// however you can update an existing array item.// ex: 'addresses.0.city': 'Charlottesville'user;user; // dynamic composition of Backbone Model [M], Collection [C] and Attribute [A]// user [M]// - addresses [C]// - 0 [M]// - city [A]// - state [A]// - 1 [M]// - city [A]// - state [A]
get()
// dot syntaxuser // returns 'VA'user // returns 'Prescott' // directuser // returns 'VA'user // returns 'Prescott'
JSON
toJSON
will decompose the Document Model/Collection into a JSON object, ready for transport.
serverInput = name: first: 'John' last: 'Doe' middle: initial: 'Z' addresses: city: 'Charlottesville' state: 'VA' city: 'Prescott' state: 'AZ' items: 123 456; user; // dynamic composition of Backbone Model [M], Collection [C] and Attribute [A]// user [M]// - name [M]// - first [A]// - last [A]// - middle [M]// - initial [A]// - addresses [C]// - 0 [M]// - city [A]// - state [A]// - 1 [M]// - city [A]// - state [A]// - items [C]// - 0 [M]// - value [A]// - 1 [M]// - value [A] // Calling `toJSON` will retrieve the composed document JSON representation.modelJSON = user; modelJSON = name: first: 'John' last: 'Doe' middle: initial: 'Z' addresses: city: 'Charlottesville' state: 'VA' city: 'Prescott' state: 'AZ' items: 123 456;
Events
"change"
"change"
events can be bound to nested attributes in the same way, and changing nested attributes will fire up the chain:
// events fired 'name.middle.initial' is set or changeduser;user;user; // all of these will fire when any address is added or changeduser;user;user;
"add" and "remove"
Additionally, nested arrays fire "add"
and "remove"
events:
// add/remove (all names are regex evaluations)user;user; user;user;
Changelog
0.6.4
- Fixes issue #7 nested array's toJSON() now returns the value.
0.6.3
- Fixes issue #6 fully reference Object.prototype.toString since certain browser's do not resolve method.
0.6.2
- Fixes issue #5 address wrapped primitives.
0.6.1
- Fixes issue #4 properly handles nested arrays.
0.6.0
- Added minimally altered backbone model/collection unit tests.
- Added readme.md specific tests.
- Updated backbone-documentmodel.js to pass unit tests.
0.5.x
- Initial release!