Skip to content

Instantly share code, notes, and snippets.

@oroce
Created January 3, 2012 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oroce/1555774 to your computer and use it in GitHub Desktop.
Save oroce/1555774 to your computer and use it in GitHub Desktop.
toJSON method for nested Backbonejs objects
var YourModel = Backbone.Model.extend({
toJSON: function(){
var obj = {};
_( this.attributes ).each( function( val, key ){
obj[ key ] = val && typeof val === "object" && "toJSON" in val ? val.toJSON() : val;
});
return obj;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment