Skip to content

Commit ea27633

Browse files
kiran3807Blair Vanderhoof
authored andcommitted
added code to enable cacheing of the gists (#55)
* added code to enable cacheing of the gists * replaced the gistCache object with gistCache array and formatted the code according to conventions
1 parent d321513 commit ea27633

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

gist-embed.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
}
2828
return lineNumbers;
2929
}
30-
30+
31+
//object to cache the calls made to the same gist-id
32+
var gistCache = {};
33+
3134
$.fn.gist = function(callback) {
3235
return this.each(function() {
3336
var $elem = $(this),
@@ -41,6 +44,7 @@
4144
hideLineNumbersOption,
4245
showLoading,
4346
showSpinner,
47+
enableCache,
4448
data = {};
4549

4650
// make block level so loading text shows properly
@@ -70,6 +74,7 @@
7074
}
7175

7276
url = 'https://gist.github.com/' + id + '.json';
77+
enableCache = $elem.data('gist-enable-cache') === true || gistCache[url];
7378
loading = 'Loading gist ' + url + (data.file ? ', file: ' + data.file : '') + '...';
7479

7580
// loading
@@ -81,15 +86,9 @@
8186
if (showSpinner) {
8287
$elem.html('<img style="display:block;margin-left:auto;margin-right:auto" alt="' + loading + '" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif">');
8388
}
84-
85-
// request the json version of this gist
86-
$.ajax({
87-
url: url,
88-
data: data,
89-
dataType: 'jsonp',
90-
timeout: 20000,
91-
success: function(response) {
92-
var linkTag,
89+
90+
function successCallback(response) {
91+
var linkTag,
9392
head,
9493
lineNumbers,
9594
highlightLineNumbers,
@@ -182,12 +181,54 @@
182181
} else {
183182
$elem.html('Failed loading gist ' + url);
184183
}
184+
}
185+
186+
function errorCallBack(textStatus) {
187+
$elem.html('Failed loading gist ' + url + ': ' + textStatus);
188+
}
189+
190+
function completeCallBack() {
191+
if (typeof callback === 'function') {
192+
callback();
193+
}
194+
}
195+
// request the json version of this gist
196+
$.ajax({
197+
url: url,
198+
data: data,
199+
dataType: 'jsonp',
200+
timeout: 20000,
201+
beforeSend : function() {
202+
// option to enable cacheing of the gists
203+
if (enableCache) {
204+
if (gistCache[url]) {
205+
// loading the response from cache and preventing the ajax call
206+
gistCache[url].then(function(response) {
207+
successCallback(response);
208+
completeCallBack();
209+
}, function(errorStatus) {
210+
errorCallBack(errorStatus);
211+
});
212+
return false;
213+
} else {
214+
// saving the promise for the requested json as a proxy for the actuall response
215+
gistCache[url] = $.Deferred();
216+
}
217+
}
218+
},
219+
success: function(response) {
220+
if (enableCache) {
221+
if (gistCache[url]) {
222+
gistCache[url].resolve(response);
223+
}
224+
}
225+
successCallback(response);
185226
},
186227
error: function(jqXHR, textStatus) {
187-
$elem.html('Failed loading gist ' + url + ': ' + textStatus);
228+
errorCallBack(textStatus);
188229
},
189230
complete: function() {
190-
if(typeof callback === 'function') callback();
231+
completeCallBack();
191232
}
192233
});
193234

0 commit comments

Comments
 (0)