Small Backbone extension for recursively calling toJSON on models and collections


310.1.33 years ago6 years agoMinified + gzip package size for @gigwalk/backbone-recursive-tojson in KB


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'});

var modelAData = modelA.toJSON();


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.


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.