comparison daily-edition.js @ 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
comparison
equal deleted inserted replaced
55:995aaa2f079a 56:35edfd76a3f7
20 function urlPolicy(url) { 20 function urlPolicy(url) {
21 return isUrlSafe(url) ? url : null; 21 return isUrlSafe(url) ? url : null;
22 }); 22 });
23 } 23 }
24 24
25 const DEFAULT_JSON_FILE = 'daily-edition.json';
26 var jsonFile = DEFAULT_JSON_FILE;
27
25 function showError() { 28 function showError() {
26 $("#error").show(); 29 var json;
27 $("#container").fadeIn("fast"); 30
31 if (jsonFile == DEFAULT_JSON_FILE)
32 json = window.localStorage[DEFAULT_JSON_FILE];
33
34 if (json)
35 showContent(json);
36 else {
37 $("#error").show();
38 $("#container").fadeIn("fast");
39 }
28 } 40 }
29 41
30 $(window).ready( 42 function showContent(json) {
31 function() {
32 var req = new XMLHttpRequest();
33 var jsonFile = 'daily-edition.json';
34
35 var matches = location.search.match(/\?issue=([0-9]+)/);
36 if (!matches)
37 matches = location.hash.match(/\#issue=([0-9]+)/);
38 if (matches)
39 jsonFile = 'issue-' + parseInt(matches[1]) + '.json';
40
41 req.open('GET', jsonFile);
42 req.overrideMimeType('text/plain');
43 req.addEventListener("error", showError, false);
44 req.addEventListener(
45 "load",
46 function(event) {
47 var info;
48
49 if (req.status != 200) {
50 showError();
51 return;
52 }
53
54 if (window.JSON) 43 if (window.JSON)
55 info = JSON.parse(req.responseText); 44 info = JSON.parse(json);
56 else 45 else
57 info = eval("(" + req.responseText + ");"); 46 info = eval("(" + json + ");");
58
59 req = null;
60 47
61 $("#issue-no").text(info.id + 1); 48 $("#issue-no").text(info.id + 1);
62 $("#pub-date").text(info.pubDate.join(".")); 49 $("#pub-date").text(info.pubDate.join("."));
63 50
64 info.authors.forEach( 51 info.authors.forEach(
93 $("#articles").append(div); 80 $("#articles").append(div);
94 }); 81 });
95 }); 82 });
96 83
97 $("#container").fadeIn("fast"); 84 $("#container").fadeIn("fast");
85 }
86
87 $(window).ready(
88 function() {
89 var req = new XMLHttpRequest();
90
91 var matches = location.search.match(/\?issue=([0-9]+)/);
92 if (!matches)
93 matches = location.hash.match(/\#issue=([0-9]+)/);
94 if (matches)
95 jsonFile = 'issue-' + parseInt(matches[1]) + '.json';
96
97 req.open('GET', jsonFile);
98 req.overrideMimeType('text/plain');
99 req.addEventListener("error", showError, false);
100 req.addEventListener(
101 "load",
102 function(event) {
103 var info;
104
105 if (req.status != 200) {
106 showError();
107 return;
108 }
109
110 if (jsonFile == DEFAULT_JSON_FILE)
111 window.localStorage[DEFAULT_JSON_FILE] = req.responseText;
112
113 showContent(req.responseText);
98 }, 114 },
99 false 115 false
100 ); 116 );
101 req.send(null); 117 req.send(null);
102 }); 118 });