Last active
August 29, 2015 14:08
-
-
Save imyelo/9f7b81e5a78471999c44 to your computer and use it in GitHub Desktop.
Better Backbone.Model/Collection.toJSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.Model.prototype.toJSON = function () { | |
var result = {}; | |
_.each(this.attributes, function (val, key) { | |
if (typeof val === 'undefined') { | |
result[key] = val; | |
} else if (typeof val.toJSON === 'function') { | |
result[key] = val.toJSON(); | |
} else if (!_.isObject(val)) { | |
result[key] = val; | |
} else { | |
result[key] = _.isArray(val) ? val.slice() : _.extend({}, val); | |
} | |
}); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment