Backbone-Nested
A plugin to make Backbone.js keep track of nested attributes. Download the latest version and see the changelog/history/release notes on the Releases page. Supports Backbone 0.9.x and 1.x.
The Need
Suppose you have a Backbone Model with nested attributes, perhaps to remain consistent with your document-oriented database. Updating the nested attribute won't cause the model's "change"
event to fire, which is confusing.
var user = new Backbone.Model({
name: {
first: 'Aidan',
last: 'Feldman'
}
});
user.bind('change', function(){
});
user.get('name').first = 'Bob';
user.save();
Wouldn't it be awesome if you could do this?
user.bind('change:name.first', function(){ ... });
Installation
Recommended.
-
Install the latest version:
bower install backbone backbone-nested-model jquery underscore --save
-
Add backbone-nested.js
to your HTML <head>
:
<script type="text/javascript" src="/bower_components/jquery/jquery.js"></script>
<script type="text/javascript" src="/bower_components/underscore/underscore.js"></script>
<script type="text/javascript" src="/bower_components/backbone/backbone.js"></script>
<script type="text/javascript" src="/bower_components/backbone-nested-model/backbone-nested.js"></script>
Manual
Download the latest release and the dependencies listed above, then include with script tags in your HTML.
Usage
-
Change your models to extend from Backbone.NestedModel
, e.g.
var Person = Backbone.Model.extend({ ... });
var Person = Backbone.NestedModel.extend({ ... });
-
Change your getters and setters to not access nested attributes directly, e.g.
user.get('name').first = 'Bob';
user.set({'name.first': 'Bob'});
Best of all, Backbone.NestedModel
is designed to be a backwards-compatible, drop-in replacement of Backbone.Model
, so the switch can be made painlessly.
Nested Attributes
get()
and set()
will work as before, but nested attributes should be accessed using the Backbone-Nested string syntax:
1-1
user.set({
'name.first': 'Bob',
'name.middle.initial': 'H'
});
user.get('name.first')
user.get('name.middle.initial')
user.set({
'name': {
first: 'Barack',
last: 'Obama'
}
});
1-N
user.set({
'addresses': [
{city: 'Brooklyn', state: 'NY'},
{city: 'Oak Park', state: 'IL'}
]
});
user.get('addresses[0].state')
user.set({
'addresses[1].state': 'MI'
});
Events
"change"
"change"
events can be bound to nested attributes in the same way, and changing nested attributes will fire up the chain:
user.bind('change', function(model, newVal){ ... });
user.bind('change:name', function(model, newName){ ... });
user.bind('change:name.middle', function(model, newMiddleName){ ... });
user.bind('change:name.middle.initial', function(model, newInitial){ ... });
user.bind('change', function(model, newVal){ ... });
user.bind('change:addresses', function(model, addrs){ ... });
user.bind('change:addresses[0]', function(model, newAddr){ ... });
user.bind('change:addresses[0].city', function(model, newCity){ ... });
"add" and "remove"
Additionally, nested arrays fire "add"
and "remove"
events:
user.bind('add:addresses', function(model, newAddr){ ... });
user.bind('remove:addresses', function(model, oldAddr){ ... });
Special Methods
add()
Acts like set()
, but appends the item to the nested array. For example:
user.get('addresses').length;
user.add('addresses', {
city: 'Seattle',
state: 'WA'
});
user.get('addresses').length;
remove()
Acts like unset()
, but if the unset item is an element in a nested array, the array will be compacted. For example:
user.get('addresses').length;
user.remove('addresses[0]');
user.get('addresses').length;
See also
Note, this plugin does not handle non-embedded relations (a.k.a. relations), which keeps it relatively simple. If you support for more complex relationships between models, see the Backbone plugin wiki page. There is also an open discussion about merging this project with backbone-deep-model.
Contributing
Pull requests are more than welcome - please add tests, which can be run by opening test/index.html. They can also be run from the command-line (requires PhantomJS):
$ npm install
$ grunt
See also: live tests for latest release.