-
Notifications
You must be signed in to change notification settings - Fork 81
/
backbone.fetch-cache.min.js
4 lines (4 loc) · 4.17 KB
/
backbone.fetch-cache.min.js
1
2
3
4
/* backbone.fetch-cache v1.2.0 (2013-10-22)
by Andy Appleton - https://github.com/mrappleton/backbone-fetch-cache.git
*/
(function(e,c){"function"==typeof define&&define.amd?define(["underscore","backbone","jquery"],function(_,Backbone,t){return e.Backbone=c(_,Backbone,t)}):e.Backbone=c(e._,e.Backbone,e.jQuery)})(this,function(_,Backbone,e){function c(c,t){var a;return(a=t&&t.url?t.url:_.isFunction(c.url)?c.url():c.url)?t&&t.data?a+"?"+e.param(t.data):a:void 0}function t(e,c,t){c=c||{};var a=Backbone.fetchCache.getCacheKey(e,c),h=!1;a&&c.cache!==!1&&(c.cache||c.prefill)&&(c.expires!==!1&&(h=(new Date).getTime()+1e3*(c.expires||300)),Backbone.fetchCache._cache[a]={expires:h,value:t},Backbone.fetchCache.setLocalStorage())}function a(e){delete Backbone.fetchCache._cache[e],Backbone.fetchCache.setLocalStorage()}function h(){if(s&&Backbone.fetchCache.localStorage)try{localStorage.setItem("backboneCache",JSON.stringify(Backbone.fetchCache._cache))}catch(e){var c=e.code||e.number||e.message;if(22!==c)throw e;this._deleteCacheWithPriority()}}function r(){if(s&&Backbone.fetchCache.localStorage){var e=localStorage.getItem("backboneCache")||"{}";Backbone.fetchCache._cache=JSON.parse(e)}}function i(e){return window.setTimeout(e,0)}var o={modelFetch:Backbone.Model.prototype.fetch,modelSync:Backbone.Model.prototype.sync,collectionFetch:Backbone.Collection.prototype.fetch},s=function(){var e=window.localStorage!==void 0;if(e)try{localStorage.setItem("test_support","test_support"),localStorage.removeItem("test_support")}catch(c){e=!1}return e}();return Backbone.fetchCache=Backbone.fetchCache||{},Backbone.fetchCache._cache=Backbone.fetchCache._cache||{},Backbone.fetchCache.priorityFn=function(e,c){return e&&e.expires&&c&&c.expires?e.expires-c.expires:e},Backbone.fetchCache._prioritize=function(){var e=_.values(this._cache).sort(this.priorityFn),c=_.indexOf(_.values(this._cache),e[0]);return _.keys(this._cache)[c]},Backbone.fetchCache._deleteCacheWithPriority=function(){Backbone.fetchCache._cache[this._prioritize()]=null,delete Backbone.fetchCache._cache[this._prioritize()],Backbone.fetchCache.setLocalStorage()},Backbone.fetchCache.localStorage===void 0&&(Backbone.fetchCache.localStorage=!0),Backbone.Model.prototype.fetch=function(c){function t(){c.parse&&(s=l.parse(s,c)),l.set(s,c),_.isFunction(c.prefillSuccess)&&c.prefillSuccess(l,s,c),l.trigger("cachesync",l,s,c),l.trigger("sync",l,s,c),c.prefill?n.notify(l):(_.isFunction(c.success)&&c.success(l,s,c),n.resolve(l))}c=_.defaults(c||{},{parse:!0});var a=Backbone.fetchCache.getCacheKey(this,c),h=Backbone.fetchCache._cache[a],r=!1,s=!1,n=new e.Deferred,l=this;return h&&(r=h.expires,r=r&&h.expires<(new Date).getTime(),s=h.value),r||!c.cache&&!c.prefill||!s||(null==c.async&&(c.async=!0),c.async?i(t):t(),c.prefill)?(o.modelFetch.apply(this,arguments).done(_.bind(n.resolve,this,this)).done(_.bind(Backbone.fetchCache.setCache,null,this,c)).fail(_.bind(n.reject,this,this)),n.promise()):n.promise()},Backbone.Model.prototype.sync=function(e,c,t){if("read"===e)return o.modelSync.apply(this,arguments);var h,r,i=c.collection,s=[];for(s.push(Backbone.fetchCache.getCacheKey(c,t)),i&&s.push(Backbone.fetchCache.getCacheKey(i)),h=0,r=s.length;r>h;h++)a(s[h]);return o.modelSync.apply(this,arguments)},Backbone.Collection.prototype.fetch=function(c){function t(){l[c.reset?"reset":"set"](s,c),_.isFunction(c.prefillSuccess)&&c.prefillSuccess(l),l.trigger("cachesync",l,s,c),l.trigger("sync",l,s,c),c.prefill?n.notify(l):(_.isFunction(c.success)&&c.success(l,s,c),n.resolve(l))}c=_.defaults(c||{},{parse:!0});var a=Backbone.fetchCache.getCacheKey(this,c),h=Backbone.fetchCache._cache[a],r=!1,s=!1,n=new e.Deferred,l=this;return h&&(r=h.expires,r=r&&h.expires<(new Date).getTime(),s=h.value),r||!c.cache&&!c.prefill||!s||(null==c.async&&(c.async=!0),c.async?i(t):t(),c.prefill)?(o.collectionFetch.apply(this,arguments).done(_.bind(n.resolve,this,this)).done(_.bind(Backbone.fetchCache.setCache,null,this,c)).fail(_.bind(n.reject,this,this)),n.promise()):n.promise()},r(),Backbone.fetchCache._superMethods=o,Backbone.fetchCache.setCache=t,Backbone.fetchCache.getCacheKey=c,Backbone.fetchCache.clearItem=a,Backbone.fetchCache.setLocalStorage=h,Backbone.fetchCache.getLocalStorage=r,Backbone});