Code Monkey home page Code Monkey logo

jquery-ajax-goodies's Introduction

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.

jquery-ajax-goodies's People

Contributors

fantactuka avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.