Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

fantactuka/jquery-ajax-goodies

Repository files navigation

jquery-ajax-goodies v0.3.1 Build Status

Adding cached (including ttl, optional local storage support) and concurrency (aborting or ignoring concurrent requests) options for better requests managing.

Installation

Using Bower bower install jquery-ajax-goodies or just copy jquery-ajax-goodies.js

Usage

Concurrency

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.

Cached

Adds cached option that allows permanent result caching if value is true.

Simple caching

$.ajax({ url: 'test', cached: true });    // Will run actual request
$.ajax({ url: 'test', cached: true });    // Returns cached jqXhr, and does not run request

Time-to-live invalidation

// Cache will be valid during next 10000 ms
$.ajax({ 
  url: 'test', 
  cached: 10000 
}); 

Date-based cache invalidation

// Cache will be valid due date
$.ajax({ 
  url: 'test', 
  cached: new Date('01/01/2014') 
});

Function cache invalidation

// 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;
  } 
}); 

Local storage

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.

About

jQuery ajax cache and concurrency extensions

Resources

License

Stars

Watchers

Forks

Packages

No packages published