Skip to content

Commit ff30c3c

Browse files
committed
cached repo data using sessionStorage zenorocha#6
1 parent 8859d43 commit ff30c3c

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Prefer a non-jquery version with pure JavaScript? No problem, [@ricardobeat](htt
4747
## History
4848

4949
* v0.2.2 January 15, 2013
50+
* Cached repo data using sessionStorage
5051
* Added error message if API exceeds its limits
5152
* v0.2.1 January 13, 2013
5253
* Added live demo

jquery.github.repos.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery Github Repos v0.3.1
2+
* jQuery Github Repos v0.2.2
33
* A jQuery plugin to display your Github Repositories.
44
* http://git.io/3A1RMg
55
*
@@ -49,6 +49,13 @@
4949
Plugin.prototype.init = function () {
5050

5151
var self = this;
52+
var cached = sessionStorage.getItem('gh-repos:' + this.repo);
53+
54+
// Attempt to get cached repo data
55+
if (window.sessionStorage && (cached != null)) {
56+
self.applyTemplate(JSON.parse(cached));
57+
}
58+
else {
5259

5360
$.ajax({
5461
url: 'https://api.github.com/repos/' + this.repo,
@@ -61,18 +68,28 @@
6168
return;
6269
}
6370
else {
71+
6472
self.applyTemplate(results.data);
73+
74+
// Cache data
75+
if (window.sessionStorage) {
76+
sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(results.data));
77+
}
78+
6579
}
6680

6781
}
6882
});
6983

84+
85+
86+
}
87+
7088
};
7189

7290
Plugin.prototype.applyTemplate = function (repo) {
7391

7492
var self = this;
75-
7693
var date = new Date(repo.pushed_at);
7794
var pushed_at = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
7895

@@ -91,13 +108,12 @@
91108
<p>' + repo.description + ' &mdash; <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \
92109
</div> \
93110
<div class="github-box-download"> \
94-
<p class="repo-update">Latest commit to <strong>master</strong> on ' + repo.pushed_at + '</p> \
111+
<p class="repo-update">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \
95112
<a class="repo-download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \
96113
</div> \
97114
</div> \
98115
');
99116

100-
// console.log($widget);
101117
self.appendTemplate($widget);
102118

103119
};

jquery.github.repos.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)