Skip to content

Instantly share code, notes, and snippets.

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 mikeymckay/0dc7f46a81adb270f05622d6d87a7cdd to your computer and use it in GitHub Desktop.
Save mikeymckay/0dc7f46a81adb270f05622d6d87a7cdd to your computer and use it in GitHub Desktop.
Adding a backbone filter to pre/post process data (useful for encoding data in the database)
originalResultSync = Result::sync
Result::sync = (method, model, options) =>
console.log method
console.log model
console.log options
if method is "update" or method is "create"
newModelAttributes = {
_id: model.get "_id"
}
newModelAttributes["_rev"] = model.get("_rev") if model.get("_rev")?
_(model.toJSON()).each (value, property) ->
return if property[0] is "_" # ignore _id, _rev, _deleted, _attachments
codedProperty = _.indexOf(mapping[model.get "question"], property)
throw "Can't find #{property} in mapping for #{model.get "question"}" if codedProperty is -1
newModelAttributes[codedProperty] = value
model.clear(silent: true)
model.set newModelAttributes
originalSuccess = options.success
options.success = (response) ->
console.log response
newResponse = {
_id: response._id
}
newResponse["_rev"] = response._rev if response._rev?
question = titleize(humanize(response._id.replace(/-.*/, "")))
_(response).each (value, codedProperty) ->
return if codedProperty[0] is "_" # ignore _id, _rev, _deleted, _attachments
decodedProperty = mapping[question][codedProperty]
newResponse[decodedProperty] = value
originalSuccess(newResponse)
#originalSuccess(response)
originalResultSync(method, model, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment