Coder Social home page Coder Social logo

jquery-memoized-ajax's Introduction

jQuery Memoized Ajax

Memoization is a technique for caching the results of expensive function calls so that subsequent calls (given the same inputs) are very fast. This property is also useful for expensive AJAX calls. This plugin adds a method to jQuery called $.memoizedAjax that behaves exactly like $.ajax, but caches the result in memory (and optionally, localStorage or sessionStorage). The next time memoizedAjax is called with the same data and url arguments, it will return the result immediately, in the form of a resolved $.Deferred. (If you have no idea what a deferred is, that's ok. Just treat this exactly like $.ajax...with a few caveats. See below.)

Install

Two options:

  • Install via bower with bower install jquery-memoized-ajax
  • Clone the repo and grab jquery.memoized.ajax.js.

This plugin is dependent on jQuery, so include this after jQuery on your page with a script tag, or use RequireJS.

Example Usage

Use it exactly how you would use $.ajax. Optionally, pass in localStorage: true or sessionStorage: true as one of the key-value pairs to cache the result in localStorage or sessionStorage. An example:

// this goes and does the ajax call and logs the result after some time
$.memoizedAjax({
  url: '/reallyFreakinSlowLookup',
  localStorage: true, // this is optional (and defaults to false)
  type: 'GET',
  data: { name: 'Bobby Tables' },
  success: function(person) { console.log(person.age); }
});

// this resolves immediately (reading from localStorage), and logs the result
$.memoizedAjax({
  url: '/reallyFreakinSlowLookup',
  localStorage: true,
  type: 'GET',
  data: { name: 'Bobby Tables' },
  success: function(person) { console.log(person.age); }
});

Caveats

Some things to watch out for when using memoizedAjax instead of regular ajax:

When memoizedAjax returns a cached result, it will actually return a resolved jQuery deferred object, not a jqXHR. Most of the time, this distinction is no big deal. There are times, however, where you'd like to abort a jqXHR object that you've stored in a variable. In those cases, you should check for the abort method before calling it, as a jQuery deferred object doesn't have an abort method, and your program will throw an error if the result happens to be cached.

var ajaxCall = $.memoizedAjax({
  url: '/reallyFreakinSlowLookup',
  type: 'GET',
  data: { name: 'Bobby Tables' }
});

// DON'T DO THIS
ajaxCall.abort(); // will throw an error if $.memoizedAjax() returns a cached result

// DO THIS INSTEAD
ajaxCall.abort && ajaxCall.abort();

One other thing to keep in mind is that accessing localStorage or sessionStorage is a blocking process. Thus, it's not a good idea to do 10 memoizedAjax calls in a row with storage enabled, as this can lock up your web page.

Advanced

Customizing storage location

By default, $.memoizedAjax will store the results of ajax calls in storage with memoizedAjax | url as the key. For example, if you do this:

$.memoizedAjax({
  url: '/really/long/url',
  localStorage: true,
  ...
});

The result is stored under localStorage['memoizedAjax | /really/long/url'].

In some cases, you will want to customize the key that memoizedAjax uses to cache the results of an ajax call. This might be useful if you want to clear the cache for specific ajax calls within your application. Use the cacheKey parameter, like this:

$.memoizedAjax({
  url: '/lookup',
  localStorage: true,
  cacheKey: 'foobar'
});

Now, the result is cached under localStorage.foobar.

Development

Clone this repo, then npm install and bower install. Tests and builds are run using gulp, so you'll need to install that with npm install gulp -g. You may need sudo.

Running gulp will watch files for changes and run tests on change. gulp build will run tests and then minify the script.

jquery-memoized-ajax's People

Contributors

erictrinh avatar jordanstephens avatar

Watchers

James Cloos avatar Carlos V. 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.