@gigwalk/backbone-recursive-tojson

Small Backbone extension for recursively calling toJSON on models and collections

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
31Mar 8, 2017Oct 6, 2013Minified + gzip package size for @gigwalk/backbone-recursive-tojson in KB

Readme

backbone-recursive-toJSON Build Status

Note: Tracis-CI reports a failing build at the moment. Somehow there is a configuration issue with Travis. All tests are passing on many browsers I've tested with.

This little helper alters the Backbone.Model.toJSON method. When this call is executed we loop through all the properties and see if there is another object that has the toJSON method. If an array is detected we will loop over all items and see if there is an object with a toJSON method.

This makes it easy to do:

var modelA = new Backbone.Model({name:'modelA'});
var modelB = new Backbone.Model({name:'modelB'});
modelA.set({otherModel:modelB});

var modelAData = modelA.toJSON();

console.log(modelAData.otherModel.name);

Be aware for circular relationships, is this will get you in a recursive loop. So for example modelA having a reference to modelB and modelB having a reference back to modelA.

Please see the specs for full details or consult Navigator-Injector-Backbone-Command-TodoMVC example as an example on how this project could be used.

Backbone.Relational

Make sure to check out Backbone.Relational too. For me it turned out this was exactly what I was looking for.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.