Code Monkey home page Code Monkey logo

jalic's Introduction

Jalic

jquery-ajax-localstorage-indexeddb-cache - abbreviated Jalic from here on, because the full name is a mouthful.

This plugin offers two major enhancements:

  1. Enhancement of the jquery ajax transports to allow for sending and receiving Blobs and ArrayBuffers using the familiar ajax methods, complete with Promise support.

  2. Caching of ajax request return values, including blobs and arraybuffers, using IndexedDB and LocalStorage (or another Storage interface compatible object).

Jalic is a plugin built for jQuery (> 1.5.1), browsers that support unprefixed/non-experimental versions of IndexedDB, and any object implementing the storage interface, such as localStorage.

It's built on a fork from Jalc, and should be considered experimental at the moment - error handling is all but absent, and many edge cases (such as using IndexedDB in Firefox Privacy Mode) are not supported with any fallbacks. Pull requests are welcome.

Usage

Parameters

	$.ajax({
		url          : '/post',
		dataType     : 'blob',      // Required if you're expecting to receive a blob or arraybuffer - no type guessing
		processData  : false,       // Required only if you're sending a blob or arraybuffer
		localCache   : true,        // Required to use caching. Can be a boolean for LocalStorage, or an object which
		                            // implements the Storage interface.
		cacheTTL     : 5,           // Optional. In hours. Default is 5.
		cacheKey     : 'cacheKey',  // Optional.
		isCacheValid : function(){  // Optional function to determine whether cache is still valid. Must return
			return true;            // synchronously. Return truthy for valid, falsey for invalid.
		}
	}).done(function(response){
	    // The response is available here.
	});

On your AJAX request you get 4 new parameters:

  • localCache
    • Turn localCache on/off, or specify an object implementing the Storage interface to use.
    • Default: false
  • cacheTTL
    • time in hours the entry should be valid.
    • only for this specific ajax request
    • Default : 5 hours
  • cacheKey
    • CacheKey is the key that will be used to store the response in localStorage.
    • Default: URL + TYPE(GET/POST) + DATA
  • isCacheValid
    • This function must return true or false (or trthy or falsey values). If falsey, the cached response is removed.
    • Default: null

Additionally, the dataType parameter can now be specified with 'blob' or 'arraybuffer'. If sending a blob or arraybuffer, remember to also set the processData parameter to false.

Notes

  • The plugin adds a new object with a Storage-like interface to the global jQuery object, $.jidb. The main difference is that all methods (e.g. setItem, removeItem, etc.) return $.Deferred promises, and execute asynchronously.
  • You can delete the cache database and its contents via $.jidb.clear().
  • You can pre-load content with this plugin. You just have do to an initial AJAX request with the same cacheKey.
  • You should specify the dataType parameter on all ajax requests.

License

This project is distributed under Mozilla Public License version 2. See LICENSE.txt for more information.

jalic's People

Contributors

basicallydan avatar bshelton229 avatar caleywoods avatar dguzzo avatar iryusa avatar mludv avatar nigelkibodeaux avatar paulirish avatar perucomsultores avatar robink avatar sanemethod avatar shaqq avatar softwarespot avatar sunnokon avatar tronhammer avatar

Watchers

 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.