Skip to content

Instantly share code, notes, and snippets.

@alexortiz201
Last active August 29, 2015 13:57
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 alexortiz201/9670816 to your computer and use it in GitHub Desktop.
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.
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