From 02d60454f5edf1a13c393a1dfd5ba0553c0a3268 Mon Sep 17 00:00:00 2001 From: kaloyane <kaloyane> Date: Wed, 9 Nov 2011 21:39:06 +0000 Subject: [PATCH] add: utility that caches JSON responses and allows offline development (without a running backend) of new-style webapps SVN: 23623 --- .../html/downloader/openbis-request-cache.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 deep_sequencing_unit/source/html/downloader/openbis-request-cache.js diff --git a/deep_sequencing_unit/source/html/downloader/openbis-request-cache.js b/deep_sequencing_unit/source/html/downloader/openbis-request-cache.js new file mode 100644 index 00000000000..f89dd844733 --- /dev/null +++ b/deep_sequencing_unit/source/html/downloader/openbis-request-cache.js @@ -0,0 +1,47 @@ +/** + * Caches openBIS JSON responses into the local browser storage. + * + * Once the local storage is populated with data from real + * server interactions, the UI development can be done offline + * based on the cached server responses. + * + * USAGE : include this script in your app *after* the openbis facade. + * Be careful not to distribute it to your customers. + * + * <script type="text/javascript" src="openbis-request-cache.js"></script> + */ + +var original_ajax_request_func = ajaxRequest; + +ajaxRequest = function(settings) { + + function getCacheId(settings) { + methodName = settings.data['method'] + if (methodName.toLowerCase().indexOf('login') != -1) { + // do not store sensitive parameters information + // for login methods (e.g. username/password) + return methodName + } else { + params = settings.data['params'] + return methodName + '-' + JSON.stringify(params) + } + } + + cacheId = getCacheId(settings) + cachedResponse = localStorage.getItem(cacheId) + + if (cachedResponse == null) { + originalCallback = settings.success + settings.success = function(response) { + localStorage.setItem(cacheId, JSON.stringify(response)) + originalCallback(response) + } + original_ajax_request_func(settings); + } else { + // async execution after a delay of 100ms + setTimeout(function() { + settings.success(JSON.parse(cachedResponse)) + }, 100) + + } +} \ No newline at end of file -- GitLab