Skip to content

Instantly share code, notes, and snippets.

@adammiller
Created February 12, 2014 22:25
Show Gist options
  • Save adammiller/8965798 to your computer and use it in GitHub Desktop.
Save adammiller/8965798 to your computer and use it in GitHub Desktop.
define ["underscore", "backbone" ], (_, Backbone) ->
FilteredAttributes =
# Instruct toJSON to use a root node
rootNode: null
# Add attribute names to filteredAttributes to exclude them from JSON
filteredAttributes: []
# Process our JSON, excluding filteredAttributes, and using
# rootNode if present
toJSON: ->
json = {}
jsonData = Backbone.Model.prototype.oldToJSON.apply @, arguments
if @filteredAttributes
jsonData = _.omit jsonData, @filteredAttributes
if @rootNode
json[@rootNode] = jsonData
else
json = jsonData
json
Backbone.Model.prototype.oldToJSON = Backbone.Model.prototype.toJSON
_.extend Backbone.Model.prototype, FilteredAttributes
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment