changeset 56:35edfd76a3f7

added localStorage-based caching for latest issue.
author Atul Varma <varmaa@toolness.com>
date Fri, 13 Aug 2010 06:04:28 +0000
parents 995aaa2f079a
children 182af9ac76fb
files cache.manifest daily-edition.js
diffstat 2 files changed, 47 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/cache.manifest	Fri Aug 13 05:44:18 2010 +0000
+++ b/cache.manifest	Fri Aug 13 06:04:28 2010 +0000
@@ -1,5 +1,5 @@
 CACHE MANIFEST
-# v12
+# v14
 daily-edition.css
 jquery.js
 html-sanitizer-minified.js
--- a/daily-edition.js	Fri Aug 13 05:44:18 2010 +0000
+++ b/daily-edition.js	Fri Aug 13 06:04:28 2010 +0000
@@ -22,41 +22,28 @@
     });
 }
 
+const DEFAULT_JSON_FILE = 'daily-edition.json';
+var jsonFile = DEFAULT_JSON_FILE;
+
 function showError() {
-  $("#error").show();
-  $("#container").fadeIn("fast");
+  var json;
+
+  if (jsonFile == DEFAULT_JSON_FILE)
+    json = window.localStorage[DEFAULT_JSON_FILE];
+
+  if (json)
+    showContent(json);
+  else {
+    $("#error").show();
+    $("#container").fadeIn("fast");
+  }
 }
 
-$(window).ready(
-  function() {
-    var req = new XMLHttpRequest();
-    var jsonFile = 'daily-edition.json';
-
-    var matches = location.search.match(/\?issue=([0-9]+)/);
-    if (!matches)
-      matches = location.hash.match(/\#issue=([0-9]+)/);
-    if (matches)
-      jsonFile = 'issue-' + parseInt(matches[1]) + '.json';
-
-    req.open('GET', jsonFile);
-    req.overrideMimeType('text/plain');
-    req.addEventListener("error", showError, false);
-    req.addEventListener(
-      "load",
-      function(event) {
-        var info;
-
-        if (req.status != 200) {
-          showError();
-          return;
-        }
-
+function showContent(json) {
         if (window.JSON)
-          info = JSON.parse(req.responseText);
+          info = JSON.parse(json);
         else
-          info = eval("(" + req.responseText + ");");
-
-        req = null;
+          info = eval("(" + json + ");");
 
         $("#issue-no").text(info.id + 1);
         $("#pub-date").text(info.pubDate.join("."));
@@ -95,6 +82,35 @@
           });
 
         $("#container").fadeIn("fast");
+}
+
+$(window).ready(
+  function() {
+    var req = new XMLHttpRequest();
+
+    var matches = location.search.match(/\?issue=([0-9]+)/);
+    if (!matches)
+      matches = location.hash.match(/\#issue=([0-9]+)/);
+    if (matches)
+      jsonFile = 'issue-' + parseInt(matches[1]) + '.json';
+
+    req.open('GET', jsonFile);
+    req.overrideMimeType('text/plain');
+    req.addEventListener("error", showError, false);
+    req.addEventListener(
+      "load",
+      function(event) {
+        var info;
+
+        if (req.status != 200) {
+          showError();
+          return;
+        }
+
+        if (jsonFile == DEFAULT_JSON_FILE)
+          window.localStorage[DEFAULT_JSON_FILE] = req.responseText;
+
+        showContent(req.responseText);
       },
       false
     );