Last active
August 29, 2015 14:24
-
-
Save rmosolgo/a2dce903d50eaad1e152 to your computer and use it in GitHub Desktop.
save key-by-key
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
| // - Request saves on a key-by-key basis | |
| // | |
| // - After the operation, record whether keys are now persisted | |
| // or whether they errored out | |
| // | |
| // - Store keys in a workflow: | |
| // _requestedKeys -> requested, but not in flight or successfully saved | |
| // _pendingKeys -> keys whose save is in flight now | |
| // _successKeys -> keys whose save was successful | |
| // _errorKeys -> keys whose save errored | |
| { | |
| // note this field name as will-save | |
| // and request a save if there isn't one already | |
| requestSave: function(fieldName) { | |
| this._requestedKeys.push(fieldName) | |
| if(this._pendingKeys.length == 0) { | |
| // if it's already saving, hold your horses | |
| this.enqueueSave() | |
| } | |
| }, | |
| // mark currently-requested keys as pending | |
| // fire the save | |
| _save: function() { | |
| this._pendingKeys = this._requestedKeys | |
| this._requestedKeys = [] | |
| this.doSave().then(function() { | |
| // after a success, mark pending keys as successful | |
| // - clear pending | |
| // - clear errors | |
| // TODO: after a timeout, clear success keys | |
| this._successKeys = this._pendingKeys | |
| this._pendingKeys = [] | |
| this._errorKeys = [] | |
| // if there were requests during this save, | |
| // allow them to have their chance | |
| if(this._requestedKeys.length) { | |
| this.enqueueSave() | |
| } | |
| }).catch(function(jqXHR)) { | |
| // after an error: | |
| // since these werent successfully saved, | |
| // they should still be considered as requested for the next save | |
| // (their new values weren't persisted) | |
| this._requestedKeys = this._requestedKeys.concat(this._pendingKeys) | |
| this._errorKeys = this._pendingKeys | |
| } | |
| }, | |
| // for displaying key-level status, | |
| // figure out where this key is in the process | |
| statusForKey: function(key) { | |
| if(this._successKeys.indexOf(key) != -1) { | |
| return 'success' | |
| } else if (this._pendingKeys.indexOf(key) != -1 || this._requestedKeys.indexOf(key) != -1) { | |
| return 'pending' | |
| } else if (this._errorKeys.indexOf(key) != -1) { | |
| return 'error' | |
| } else { | |
| return null | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment