Created
June 14, 2017 08:40
-
-
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)
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
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