Last active
August 29, 2015 13:57
-
-
Save alexortiz201/9670816 to your computer and use it in GitHub Desktop.
Buffer function for saving to DB, this prevents chattiness across the wire by waiting till user stops interacting with model.
This file contains 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
RV.Profile.prototype.save = function () { | |
var delay = 3000, | |
self = this; | |
function saveProfile(args) { | |
self = args[0]; | |
// cache | |
args[0].saveDB().then(function () { | |
RV.utils.profileLocalStore.updateNEW(self); | |
}); | |
} | |
if (this.saveTimer !== null) { | |
window.clearTimeout(this.saveTimer); | |
} | |
this.saveTimer = window.setTimeout(saveProfile, delay, [self]); | |
}; | |
RV.Profile.prototype.saveDB = function () { | |
// assert | |
if (this.id !== RV.ProfileId()) { | |
console.log('ERROR WITH NON-MATCHING IDs - ID PASSED: ' + this.id); | |
RV.utils.showNoty('Your message was not saved, wrong Profile Id', 'top', 'error', 5000); | |
return false; | |
} | |
var jsPromise = Promise.cast($.ajax({ | |
url: '/settings/profile', | |
type: 'POST', | |
dataType: 'json', | |
contentType: 'application/json; charset=utf-8', | |
data: JSON.stringify(this) | |
})); | |
return jsPromise; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment