From d951380b4048a2d88878e628dcd452d72579a96b Mon Sep 17 00:00:00 2001 From: jlukic Date: Wed, 10 Jun 2015 17:38:00 -0400 Subject: [PATCH] Fix API to use session storage for caching values so they are available across a session --- src/definitions/behaviors/api.js | 48 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/definitions/behaviors/api.js b/src/definitions/behaviors/api.js index 8b89775f8..d53b6f1d9 100644 --- a/src/definitions/behaviors/api.js +++ b/src/definitions/behaviors/api.js @@ -76,7 +76,6 @@ $.api = $.fn.api = function(parameters) { initialize: function() { if(!methodInvoked) { - module.create.cache(); module.bind.events(); } module.instantiate(); @@ -121,28 +120,39 @@ $.api = $.fn.api = function(parameters) { var response ; - if(!module.cache) { - module.create.cache(); + if(window.Storage === undefined) { + module.error(error.noStorage); + return; } - response = (module.cache.response[url] !== undefined) - ? module.cache.response[url] - : false - ; + response = sessionStorage.getItem(url); module.debug('Using cached response', url, response); - return response; + if(response !== undefined) { + try { + response = JSON.parse(response); + } + catch(e) { + // didnt store object + } + return response; + } + return false; } }, write: { cachedResponse: function(url, response) { - if(!module.cache) { - module.create.cache(); - } if(response && response === '') { module.debug('Response empty, not caching', response); return; } + if(window.Storage === undefined) { + module.error(error.noStorage); + return; + } + if( $.isPlainObject(response) ) { + response = JSON.stringify(response); + } + sessionStorage.setItem(url, response); module.verbose('Storing cached response for url', url, response); - module.cache.response[url] = response; } }, @@ -518,12 +528,6 @@ $.api = $.fn.api = function(parameters) { create: { - cache: function() { - module.verbose('Creating local response cache'); - module.cache = { - response: {} - }; - }, // api promise request: function() { @@ -552,7 +556,12 @@ $.api = $.fn.api = function(parameters) { else if( $.isFunction(settings.mockResponseAsync) ) { callback = function(response) { module.verbose('Async callback returned response', response); - module.request.resolveWith(context, [response]); + if(response) { + module.request.resolveWith(context, [response]); + } + else { + module.request.rejectWith(context, [true]); + } }; module.debug('Using async mocked response', settings.mockResponseAsync); settings.mockResponseAsync.call(context, settings, callback); @@ -968,6 +977,7 @@ $.api.settings = { missingSerialize : 'Required dependency jquery-serialize-object missing, using basic serialize', missingURL : 'No URL specified for api event', noReturnedValue : 'The beforeSend callback must return a settings object, beforeSend ignored.', + noStorage : 'Caching respopnses locally requires session storage', parseError : 'There was an error parsing your request', requiredParameter : 'Missing a required URL parameter: ', statusMessage : 'Server gave an error: ',