-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-fetch.min.js
8 lines (6 loc) · 1.58 KB
/
jquery-fetch.min.js
1
2
3
4
5
6
7
8
/***
jquery-fetch 0.0.1
Wraps ajax calls, adding cache storage support and unlike $.ajax() it ALWAYS retuns a promise
Copyright 2011 Francois Lafortune, quickredfox <code@quickredfox.at> (http://about.me/quickredfox)
***/
(function(){var a,b,c,d,e;c=typeof process!="undefined"&&process.versions&&!!process.versions.node,c?a=d=require("jquery"):a=d=window.jQuery,e=new function(){var a;a={},this.clear=function(){this.length=0,a={};return this},this.getItem=function(b){return a[b]},this.setItem=function(b,c){this.length++;return a[b]=c.toString()},this.removeItem=function(b){this.length--;return delete a[b]};return this.clear()},b=function(a){var b;b=Object.getPrototypeOf(a),b.getItemJSON||(b.getItemJSON=function(b){var c;c=a.getItem(b);if(typeof c!="string")return JSON.stringify(c);return JSON.parse(c)}),b.setItemJSON||(b.setItemJSON=function(b,c){c=JSON.stringify(c);return a.setItem(b,c)});return a},a.fetch=function(c){var d,f,g,h,i,j,k;c||(c={}),i=a.Deferred(),k=c.url||"/",e=b(c.store||e),c.success||(c.success=a.noop),c.complete||(c.complete=a.noop),c.error||(c.error=a.noop),j=e.getItemJSON(k),d=a.fetchRequests[k],h=function(a){e.setItemJSON(k,{response:a,time:(new Date).getTime()}),i.resolve(a);return i},f=function(a){i.rejectWith(null,arguments);return i.promise()};if(j&&j.response){j.time+a.fetchExpiry>=(new Date).getTime()&&e.removeItem(k);return h(j.response)}if(d)d.then(h,f);else{try{g=a.ajax(c)}catch(l){return f(l)}g===!1?f("broken promise"):(a.fetchRequests[k]=g,g.then(h,f))}return i.promise()},a.fetchRequests={},a.fetchExpiry=18e5,c&&(module.exports=a.fetch)}).call(this)