From 87cbe3dcd4159c6ddbc76a8be27c0b989d70dc76 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sat, 20 Sep 2014 15:32:03 +0100 Subject: Store the data in localStorage It will currently be refreshed after 1 week. --- src/leaflet-soton.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/leaflet-soton.js b/src/leaflet-soton.js index ac5754b..65e8e6e 100644 --- a/src/leaflet-soton.js +++ b/src/leaflet-soton.js @@ -12,6 +12,8 @@ workstationData: null, updateWorkstationData: true, // regularly poll for workstation data workstationDataUpdateTime: 30000, // time to wait between requests + useLocalStorage: true, + localStorageTimeout: 604800000, // one day in milliseconds _workstationDataFetchInProgress: false, _workstationDataFetchTimeout: null, @@ -21,9 +23,30 @@ } else { this.on("dataload", callback); + if (LS.useLocalStorage && + 'localStorage' in window && window['localStorage'] !== null) { + + if ("data" in localStorage) { + var refetchTime = localStorage.dataTimestamp + LS.localStorageTimeout; + + if (refetchTime > new Date().getTime()) { + LS.data = JSON.parse(localStorage.data); + + LS.fire("dataload", LS.data); + + return; + + } + + // refresh the data, as its too old + } + + // data not in local storage, so fetch it + } + if (!this._dataFetchInProgress) { this._dataFetchInProgress = true; - getJSON({url: LS.dataPath, cache: false} , function(data) { + getJSON({url: LS.dataPath, cache: false} , function(data, stringData) { LS._dataFetchInProgress = false; if (data === null) { @@ -35,6 +58,12 @@ LS.data = data; + if (LS.useLocalStorage && + 'localStorage' in window && window['localStorage'] !== null) { + localStorage.data = stringData; + localStorage.dataTimestamp = new Date().getTime(); + } + LS.fire("dataload", data); }); } @@ -1648,7 +1677,7 @@ SELECT * WHERE {\ xhttp.send(options.data); xhttp.onreadystatechange = function() { if (xhttp.status == 200 && xhttp.readyState == 4) { - callback(JSON.parse(xhttp.responseText)); + callback(JSON.parse(xhttp.responseText), xhttp.responseText); } }; } -- cgit v1.2.3