Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 036ec7297ee0dc85b4c9249f010f1440097d37b0 Maxime Brazeilles committed Jul 11, 2011
Binary file not shown.
@@ -0,0 +1,13 @@
+Copyright 2011 Nectify
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
No changes.
No changes.
@@ -0,0 +1,41 @@
+# About
+jquery-ajax-jstorage-cache is a plugin build for jQuery (>1.5.1) and "jStorage":http://www.jstorage.info . It's a client-side cache plugin for AJAX response intended to save bandwith and time. All the responses are stored in the Local Storage powered by HTML5 ( see "jStorage#Support":http://www.jstorage.info/#support ).
+
+# How to use
+
+## Parameters
+
+ $.ajax({
+ url: '/post',
+
+ cache: true,
+ cacheKey: 'post',
+ isCacheValid: function(){
+ return true;
+ },
+
+ success: function(reply) {
+ // i can play with my reply !
+ }
+ });
+
+On your AJAX request you got 3 new parameters :
+
+* cache
+** Turn cache on/off
+** Default: false
+* cacheKey
+** CacheKey is the key that will be used to store the response in jStorage. It allow you to delete your cache easily with the jStorage.removeKey() function.
+** Default: URL + TYPE(GET/POST) + DATA
+* isCacheValid
+** This function must return true or false. On false, the cached response is removed.
+** Default: null
+
+## Notes
+
+* You can delete the cache by using jStorage API ( see "jStorage#Usage":http://www.jstorage.info/#usage ).
+* 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
+
+This project is distributed under Apache 2 License. See LICENSE.txt for more information.
@@ -0,0 +1,38 @@
+$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
+ // Cache it ?
+ if( ! options.cache )
+ return;
+
+ var cacheKey;
+ // If cacheKey exist we take it, or default one will be used
+ if ( options.cacheKey )
+ cacheKey = options.cacheKey;
+ else
+ cacheKey = options.url + options.type + options.data;
+
+ // isCacheValid is a function to validate cache
+ if( options.isCacheValid && ! options.isCacheValid() ){
+ $.jStorage.deleteKey( cackeKey );
+ }
+
+ if( $.jStorage.get ( cacheKey ) ){
+ //In the cache? So get it, apply success callback & abort the XHR request
+ options.success( $.jStorage.get ( cacheKey ) );
+ // Abort is broken on JQ 1.5 :(
+ jqXHR.abort();
+ }else{
+ //If it not in the cache, we change the success callback, just put data on jStorage and after that apply the initial callback
+ if( options.success ) {
+ var successhandler = options.success;
+
+ options.success = function( data ) {
+ $.jStorage.set( cacheKey, data );
+ successhandler( data );
+ }
+ }else{
+ options.success = function( data ) {
+ $.jStorage.set( cacheKey, data );
+ }
+ }
+ }
+});
Oops, something went wrong.

0 comments on commit 036ec72

Please sign in to comment.