Last active
August 29, 2015 14:27
-
-
Save bunnymatic/4cb28555860355660150 to your computer and use it in GitHub Desktop.
jQuery Cached Ajax Data Service
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
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