Adding cached
(including ttl, optional local storage support) and concurrency
(aborting or ignoring concurrent requests) options for better requests managing.
Using Bower bower install jquery-ajax-goodies
or just copy jquery-ajax-goodies.js
Adds concurrency
option that allows to manage concurrent requests.
Example:
$.ajax({ url: '/test', concurrency: 'suppress' });
$.ajax({ url: '/test', concurrency: 'suppress' }); // If previous request is not finished yet, will abort it
$.ajax({ url: '/test', concurrency: 'abort' });
$.ajax({ url: '/test', concurrency: 'abort' }); // If previous request is not finished yet, will abort new one
Concurrent requests are stored by request key, that is built from method (get, post), url and data (get params), but it also possible to define own key for cases when request should manage concurrents, but might use different data. E.g. when requesting for search or suggestions.
$.ajax({ url: '/search', data: {q: 'a'}, concurrency: { key: 'search', type: 'suppress' } });
$.ajax({ url: '/search', data: {q: 'b'}, concurrency: { key: 'search', type: 'suppress' } });
In example above requests would be concurrent event though they have a different set of data, since we specified concurrency key.
Adds cached
option that allows permanent result caching if value is true.
$.ajax({ url: 'test', cached: true }); // Will run actual request
$.ajax({ url: 'test', cached: true }); // Returns cached jqXhr, and does not run request
// Cache will be valid during next 10000 ms
$.ajax({
url: 'test',
cached: 10000
});
// Cache will be valid due date
$.ajax({
url: 'test',
cached: new Date('01/01/2014')
});
// Cache will be valid if function returns non-falsy value.
// `data` is cached response, stamp - cache time stamp
$.ajax({
url: 'test',
cached: function(data, stamp) {
....
return result;
}
});
By default cache is a run-time object, that means when you reload the page — it's invalidated. You're able to override default cache adapter to support local storage functionality:
$.ajax.goodies.cached.setAdapter({
setItem: function(key, value) {
localStorage.setItem(key, JSON.stringify(value));
},
getItem: function(key) {
var value = localStorage.getItem(key);
return JSON.parse(value);
},
removeItem: function(key) {
localStorage.removeItem(key);
}
});
Note that adapter should implement getItem
, setItem
, removeItem
methods to be fully functional.