diff options
author | Christopher Baines <cb15g11@soton.ac.uk> | 2014-09-20 15:32:03 +0100 |
---|---|---|
committer | Christopher Baines <cb15g11@soton.ac.uk> | 2014-09-20 15:32:03 +0100 |
commit | 87cbe3dcd4159c6ddbc76a8be27c0b989d70dc76 (patch) | |
tree | 05987b057061da6a49d9282636eda548524da3d8 | |
parent | c9f80f3d8fd3ec2d00c588dc7bacf0dbc329eda8 (diff) | |
download | leaflet-soton-87cbe3dcd4159c6ddbc76a8be27c0b989d70dc76.tar leaflet-soton-87cbe3dcd4159c6ddbc76a8be27c0b989d70dc76.tar.gz |
Store the data in localStorage
It will currently be refreshed after 1 week.
-rw-r--r-- | src/leaflet-soton.js | 33 |
1 files 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); } }; } |