Add Rails-like nested attributes support for Backbone.Model.
Add this line to your application's Gemfile:
gem 'backbone-nested-attributes'
And then execute:
$ bundle
Or install it yourself as:
$ gem install backbone-nested-attributes
Then, add this line in your application.js:
//= require backbone-nested-attributes/all
Make your model extend from Backbone.NestedAttributesModel, instead of Backbone.Model and declare your relationships:
varPost=Backbone.NestedAttributesModel.extend({relations: [{type: 'one',key: 'author',relatedModel: function(){returnPerson}},{key: 'comments',relatedModel: function(){returnComment}}]})varComment=Backbone.NestedAttributesModel.extend({})varPerson=Backbone.NestedAttributesModel.extend({})Now you can create your posts like this:
varpost=newPost({id: 123,title: 'My Title',author: {id: 987,name: "Vicente Mundim"},comments: [{id: 765,body: "Nice writeup!"},{id: 766,body: "Keep it going!"}]})post.get('author')// returns a Person modelpost.get('comments')// returns a Backbone.Collection of Comment modelsWhen saving data, you can choose whether to send attributes as usual, or with nested attributes support by giving { nested: true } to save:
post.save({},{nested: true})This will send data to the server like this:
{id: 123,title: 'My Title',author_attributes: {id: 987,name: "Vicente Mundim"},comments_attributes: [{id: 765,body: "Nice writeup!"},{id: 766,body: "Keep it going!"}]}It keeps track of deleted models in 1-N relations:
varcomment=post.get('comments').at(0)post.get('comments').remove(comment)post.save({},{nested: true})Send this data to the server:
{id: 123,title: 'My Title',author_attributes: {id: 987,name: "Vicente Mundim"},comments_attributes: [{id: 765,body: "Nice writeup!",_destroy: true},{id: 766,body: "Keep it going!"}]}You can whitelist attributes to serialize using serialize_keys
varPost=Backbone.NestedAttributesModel.extend({relations: [{key: 'comments',serialize_keys: ['author','content'],relatedModel: function(){returnComment}}]})The name of the attribute set on a destroyed model can be changed using destroy_action
varPost=Backbone.NestedAttributesModel.extend({relations: [{key: 'comments',destroy_action: '_remove',relatedModel: function(){returnComment}}]})This allow you to call another method on the model instead of destroy.
If you're using some bind plugin and you want to cancel changes that were made without reloading the page or hitting the backend you'll definitively want to take a look at Backbone.UndoableModel:
varPost=Backbone.UndoableModel.extend({relations: [// UndoableModel is a NestedAttributesModel, so it can have relations{type: 'one',key: 'author',relatedModel: function(){returnPerson}},{key: 'comments',relatedModel: function(){returnComment}}]})varComment=Backbone.UndoableModel.extend({})varPerson=Backbone.UndoableModel.extend({})varpost=newPost({id: 123,title: 'My Title',author: {id: 987,name: "Vicente Mundim"},comments: [{id: 765,body: "Nice writeup!"},{id: 766,body: "Keep it going!"}]})post.set({title: 'My new title'})post.get('author').set({name: 'Jon Snow'})post.get('comments').at(0).set({body: 'Great post!'})post.undo()// that's it, post is now reverted to its initial attributes, as well as its relationspost.get('title')// 'My Title'post.get('author').get('name')// 'Vicente Mundim'post.get('comments').at(0).get('body')// "Nice writeup!"Check out the specs:
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request

