Skip to content

Instantly share code, notes, and snippets.

@bunnymatic
Last active August 29, 2015 14:27
Show Gist options
  • Save bunnymatic/4cb28555860355660150 to your computer and use it in GitHub Desktop.
Save bunnymatic/4cb28555860355660150 to your computer and use it in GitHub Desktop.
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