Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery Cached Ajax Data Service
window.CachedDataService = {
inProgress: {
},
ajax: function(ajaxOptions) {
var key = ajaxOptions.url + (JSON.stringify(ajaxOptions.data || ''))
if (this.inProgress[key]) {
return this.inProgress[key];
}
else {
this.inProgress[key] = $.ajax(ajaxOptions);
return this.inProgress[key];
}
}
}
/** sample call */
var ajaxOpts = {
url: '/big_data_fetch'
}
var success = function(data) {
// do something with the data
}
var complete = function() {
// do something after the fetch is finished
}
CachedDataService.ajax().done(success).always(complete)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment