|
27 | 27 | }
|
28 | 28 | return lineNumbers;
|
29 | 29 | }
|
30 |
| - |
| 30 | + |
| 31 | + //object to cache the calls made to the same gist-id |
| 32 | + var gistCache = {}; |
| 33 | + |
31 | 34 | $.fn.gist = function(callback) {
|
32 | 35 | return this.each(function() {
|
33 | 36 | var $elem = $(this),
|
|
41 | 44 | hideLineNumbersOption,
|
42 | 45 | showLoading,
|
43 | 46 | showSpinner,
|
| 47 | + enableCache, |
44 | 48 | data = {};
|
45 | 49 |
|
46 | 50 | // make block level so loading text shows properly
|
|
70 | 74 | }
|
71 | 75 |
|
72 | 76 | url = 'https://gist.github.com/' + id + '.json';
|
| 77 | + enableCache = $elem.data('gist-enable-cache') === true || gistCache[url]; |
73 | 78 | loading = 'Loading gist ' + url + (data.file ? ', file: ' + data.file : '') + '...';
|
74 | 79 |
|
75 | 80 | // loading
|
|
81 | 86 | if (showSpinner) {
|
82 | 87 | $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">');
|
83 | 88 | }
|
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, |
93 | 92 | head,
|
94 | 93 | lineNumbers,
|
95 | 94 | highlightLineNumbers,
|
|
182 | 181 | } else {
|
183 | 182 | $elem.html('Failed loading gist ' + url);
|
184 | 183 | }
|
| 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); |
185 | 226 | },
|
186 | 227 | error: function(jqXHR, textStatus) {
|
187 |
| - $elem.html('Failed loading gist ' + url + ': ' + textStatus); |
| 228 | + errorCallBack(textStatus); |
188 | 229 | },
|
189 | 230 | complete: function() {
|
190 |
| - if(typeof callback === 'function') callback(); |
| 231 | + completeCallBack(); |
191 | 232 | }
|
192 | 233 | });
|
193 | 234 |
|
|
0 commit comments