Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from caleywoods/master

Syntax Highlighting to README
  • Loading branch information...
commit b430f590d9dc98f436bcc8d80ee84b6ef9fa2841 2 parents 7fa0c55 + f6eb7b9
@paulirish paulirish authored
Showing with 21 additions and 23 deletions.
  1. +9 −10 README.md
  2. +12 −13 jquery-ajax-localstorage-cache.js
View
19 README.md
@@ -4,14 +4,14 @@ jquery-ajax-localstorage-cache is a plugin built for jQuery (>1.5.1) and localSt
# How to use
## Parameters
-
+```javascript
$.ajax({
- url: '/post',
- localCache: true, // required to use
+ url : '/post',
+ localCache : true, // required to use
- cacheTTL : 1, // in hours. Optional
- cacheKey: 'post', // optional
- isCacheValid: function(){ // optional
+ cacheTTL : 1, // in hours. Optional
+ cacheKey : 'post', // optional
+ isCacheValid : function(){ // optional
return true;
},
@@ -19,8 +19,8 @@ jquery-ajax-localstorage-cache is a plugin built for jQuery (>1.5.1) and localSt
// i can play with my reply !
}
});
-
-On your AJAX request you got 4 new parameters :
+```
+On your AJAX request you get 4 new parameters :
* localCache
* Turn localCache on/off
@@ -36,10 +36,9 @@ On your AJAX request you got 4 new parameters :
* This function must return true or false. On false, the cached response is removed.
* Default: null
-
## Notes
-* You can delete the cache by using `localStorage.clear()`.
+* You can delete the cache by using ```localStorage.clear()```.
* Note that you can pre-load content with this plugin. You just have do to the same AJAX request without a success callback and the same cacheKey.
# License
View
25 jquery-ajax-localstorage-cache.js
@@ -3,36 +3,35 @@
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
-
// Cache it ?
- if( !Modernizr.localstorage || !options.localCache ) return;
+ if ( !Modernizr.localstorage || !options.localCache ) return;
var hourstl = options.cacheTTL || 5;
var cacheKey = options.cacheKey ||
- options.url.replace(/jQuery.*/,'') + options.type + options.data;
+ options.url.replace( /jQuery.*/,'' ) + options.type + options.data;
// isCacheValid is a function to validate cache
- if( options.isCacheValid && ! options.isCacheValid() ){
+ if ( options.isCacheValid && ! options.isCacheValid() ){
localStorage.removeItem( cacheKey );
}
// if there's a TTL that's expired, flush this item
var ttl = localStorage.getItem(cacheKey + 'cachettl');
- if (ttl && ttl < +new Date()){
+ if ( ttl && ttl < +new Date() ){
localStorage.removeItem( cacheKey );
- localStorage.removeItem( cacheKey + 'cachettl');
+ localStorage.removeItem( cacheKey + 'cachettl' );
ttl = 'expired';
}
var value = localStorage.getItem( cacheKey );
- if( value ){
+ if ( value ){
//In the cache? So get it, apply success callback & abort the XHR request
// parse back to JSON if we can.
- if (options.dataType.indexOf('json') === 0) value = JSON.parse(value);
+ if ( options.dataType.indexOf( 'json' ) === 0 ) value = JSON.parse( value );
options.success( value );
// Abort is broken on JQ 1.5 :(
jqXHR.abort();
- }else{
+ } else {
//If it not in the cache, we change the success callback, just put data on localstorage and after that apply the initial callback
if ( options.success ) {
@@ -40,14 +39,14 @@ $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
}
options.success = function( data ) {
var strdata = data;
- if (this.dataType.indexOf('json') === 0) strdata = JSON.stringify(data);
+ if ( this.dataType.indexOf( 'json' ) === 0 ) strdata = JSON.stringify( data );
localStorage.setItem( cacheKey, strdata );
- if (options.realsuccess) options.realsuccess( data );
+ if ( options.realsuccess ) options.realsuccess( data );
};
// store timestamp
- if (!ttl || ttl === 'expired'){
- localStorage.setItem( cacheKey + 'cachettl', +new Date() + 1000 * 60 * 60 * hourstl);
+ if ( ! ttl || ttl === 'expired' ) {
+ localStorage.setItem( cacheKey + 'cachettl', +new Date() + 1000 * 60 * 60 * hourstl );
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.