diff --git a/request/amplify.request.js b/request/amplify.request.js index 5e84941..64775a3 100644 --- a/request/amplify.request.js +++ b/request/amplify.request.js @@ -199,7 +199,7 @@ if ( amplify.store ) { } var success = ajaxSettings.success; ajaxSettings.success = function( data ) { - amplify.store[ type]( cacheKey, data , { expires: resource.cache.expires } ); + amplify.store[ type ]( cacheKey, data, { expires: resource.cache.expires } ); success.apply( this, arguments ); }; }; @@ -210,9 +210,8 @@ if ( amplify.store ) { amplify.subscribe( "request.before.ajax", function( resource ) { var cacheType = resource.cache; if ( cacheType ) { - if ( typeof cacheType === 'object' ) { - cacheType = cacheType.type; - } + // normalize between objects and strings/booleans/numbers + cacheType = cacheType.type || cacheType; return cache[ cacheType in cache ? cacheType : "_default" ] .apply( this, arguments ); } diff --git a/request/test/unit.js b/request/test/unit.js index 86567b7..fd45926 100644 --- a/request/test/unit.js +++ b/request/test/unit.js @@ -835,7 +835,7 @@ asyncTest( "cache: Number", function() { }); if ( amplify.store ) { - asyncTest( "cache: persist - no expires", { + var storeExpiresLifecycle = { setup: function() { $.each( amplify.store(), function( key ) { if ( /^request/.test( key ) ) { @@ -843,7 +843,8 @@ if ( amplify.store ) { } }); } - }, function() { + }; + asyncTest( "cache: persist - no expires", storeExpiresLifecycle, function() { expect( 15 ); var ajaxCalls = 0; @@ -887,80 +888,71 @@ if ( amplify.store ) { }); }); }); - - asyncTest( "cache: persist - with cache options", { - setup: function() { - $.each( amplify.store(), function( key ) { - if ( /^request/.test( key ) ) { - amplify.store( key, null ); - } - }); - } - }, function() { - expect( 17 ); - - var ajaxCalls = 0; + + asyncTest( "cache: persist - expires", storeExpiresLifecycle, function() { + expect( 24 ); + + var shouldCache = false; $( "#ajax-listener" ).ajaxComplete(function( event, xhr ) { if ( xhr.statusText !== "abort" ) { - ok( !ajaxCalls++, "ajax call completed" ); + ok( !shouldCache, "ajax call completed" ); } }); - + amplify.request.define( "persist-cache", "ajax", { url: "data/data.json", dataType: "json", - cache: { type: 'persist', expires: 450 } + cache: { type: "persist", expires: 450 } }); - - // should execute for both requests + + // should execute for 3 requests subscribe( "request.before", function( settings ) { equal( settings.resourceId, "persist-cache", "before message: settings.resourceId" ); }); - // should execute for first request only + // should execute for 2 requests subscribe( "request.before.ajax", function( resource ) { equal( resource.resourceId, "persist-cache", "before.ajax message: resource.resourceId" ); equal( resource.cache.expires, 450, "before.ajax message: resource.expires"); }); - // should execute for both requests + // should execute 3 requests subscribe( "request.success", function( settings, data ) { equal( settings.resourceId, "persist-cache", "success message: settings.resourceId" ); deepEqual( data, { foo: "bar" }, "success message: data" ); }); - // should execute for both requests + // should execute for 3 requests subscribe( "request.complete", function( settings, data ) { equal( settings.resourceId, "persist-cache", "complete message: settings.resourceId" ); - deepEqual( data, { foo: "bar" }, "complete message: data" ); + deepEqual( data, { foo: "bar" }, "complete message: data" ); }); subscribe( "request.error", function() { ok( false, "error message published" ); }); - + amplify.request( "persist-cache", function( data ) { + // delay setting the flag because the success callback will be invoked + // before the ajaxComplete event is triggered + setTimeout(function() { + shouldCache = true; + }, 1 ); deepEqual( data, { foo: "bar" }, "first request callback" ); amplify.request( "persist-cache", function( data ) { deepEqual( data, { foo: "bar" }, "second request callback" ); }); + setTimeout(function() { + shouldCache = false; + amplify.request( "persist-cache", function( data ) { + deepEqual( data, { foo: "bar" }, "third request callback" ); + start(); + }); + }, 500 ); }); - - setTimeout( function() { - deepEqual( amplify.store( amplify.request.cache._key( "persist-cache", "" ) ), { foo: "bar" }, "data not expired: 300" ); - }, 300 ); - - setTimeout( function() { - deepEqual( amplify.store( amplify.request.cache._key( "persist-cache", "" ) ), undefined, "data should be expired: 500" ); - start(); - }, 500 ); - - - }); - + test( "cache types", function() { $.each( amplify.store.types, function( type ) { ok( type in amplify.request.cache, type ); }); }); - } asyncTest( "decoder: Function - success", function() {