Browse Source

Adjust cache type to be called local to allow for standard caching choices

pull/2300/head
jlukic 9 years ago
parent
commit
421fdadd76
1 changed files with 57 additions and 23 deletions
  1. 80
      src/definitions/behaviors/api.js

80
src/definitions/behaviors/api.js

@ -115,6 +115,36 @@ $.api = $.fn.api = function(parameters) {
}
},
read: {
cachedResponse: function(url) {
var
response
;
if(!module.cache) {
module.create.cache();
}
response = (module.cache.response[url] !== undefined)
? module.cache.response[url]
: false
;
module.debug('Using cached response', url, response);
return response;
}
},
write: {
cachedResponse: function(url, response) {
if(!module.cache) {
module.create.cache();
}
if(response && response === '') {
module.debug('Response empty, not caching', response);
return;
}
module.verbose('Storing cached response for url', url, response);
module.cache.response[url] = response;
}
},
query: function() {
if(module.is.disabled()) {
@ -197,27 +227,26 @@ $.api = $.fn.api = function(parameters) {
module.verbose('Using AJAX settings', ajaxSettings);
// pull from cache
if(settings.cache && module.cache.response[url] !== undefined) {
module.debug('Pulling response from cache');
if(settings.cache === 'local' && module.read.cachedResponse(url)) {
module.request = module.create.request();
module.request.resolveWith(context, [ module.cache.response[url] ]);
module.request.resolveWith(context, [ module.read.cachedResponse(url) ]);
return;
}
if( module.is.loading() ) {
// throttle additional requests
if( !module.is.loading() ) {
module.request = module.create.request();
module.xhr = module.create.xhr();
settings.onRequest.call(context, module.request, module.xhr);
}
else {
// throttle repeated api requests
module.debug('Repeated request throttled', settings.throttle);
module.timer = setTimeout(function() {
module.request = module.create.request();
module.xhr = module.create.xhr();
settings.onRequest.call(context, module.request, module.xhr);
}, settings.throttle);
}
else {
// immediately on first request
module.request = module.create.request();
module.xhr = module.create.xhr();
settings.onRequest.call(context, module.request, module.xhr);
}
},
@ -368,9 +397,7 @@ $.api = $.fn.api = function(parameters) {
// page triggers abort on navigation, dont show error
setTimeout(function() {
if(xhr.readyState !== undefined && xhr.readyState === 0) {
module.debug('Request Aborted (Most likely caused by page navigation or CORS Policy)', status, httpMessage);
settings.onAbort.call(context, status, $module);
module.reset();
module.request.rejectWith(context, [xhr, 'aborted', httpMessage]);
}
else {
module.request.rejectWith(context, [xhr, status, httpMessage]);
@ -391,9 +418,9 @@ $.api = $.fn.api = function(parameters) {
;
module.debug('API Response Received', response);
if(settings.cache && url) {
module.cache.response[url] = response;
module.debug('Caching response for next lookup', module.cache);
if(settings.cache === 'local' && url) {
module.write.cachedResponse(url, response);
module.debug('Adding url to local cache', module.cache);
}
if(translatedResponse) {
@ -420,16 +447,23 @@ $.api = $.fn.api = function(parameters) {
settings.onSuccess.call(context, response, $module);
}
},
error: function(xhr, status, httpMessage) {
fail: function(xhr, status, httpMessage) {
var
errorMessage = (settings.error[status] !== undefined)
? settings.error[status]
: httpMessage,
response
;
// let em know unless request aborted
if(xhr !== undefined) {
// request aborted, don't show error state
if(status == 'aborted') {
module.debug('Request Aborted (Most likely caused by page navigation or CORS Policy)', status, httpMessage);
module.reset();
settings.onAbort.call(context, status, $module);
return;
}
if(xhr !== undefined) {
// if http status code returned and json returned error, look for it
if( xhr.status != 200 && httpMessage !== undefined && httpMessage !== '') {
module.error(error.statusMessage + httpMessage, ajaxSettings.url);
@ -448,7 +482,7 @@ $.api = $.fn.api = function(parameters) {
}
}
module.remove.loading();
// show error state only for duration specified in settings
// show error state if specified with length
if(settings.errorDuration !== false) {
module.set.error();
setTimeout(module.remove.error, settings.errorDuration);
@ -474,7 +508,7 @@ $.api = $.fn.api = function(parameters) {
return $.Deferred()
.always(module.event.request.complete)
.done(module.event.request.done)
.fail(module.event.request.error)
.fail(module.event.request.fail)
;
},
// xhr promise
@ -849,7 +883,7 @@ $.api.settings = {
performance : true,
// cache
cache : false,
cache : 'local',
// event binding
on : 'auto',

Loading…
Cancel
Save