forked from GerHobbelt/backbone-fetch-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.fetch-cache.min.js
4 lines (4 loc) · 4.14 KB
/
backbone.fetch-cache.min.js
1
2
3
4
/* backbone.fetch-cache v1.1.2 (2013-09-26)
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(n&&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(n&&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},n=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(){l.set(n,c),_.isFunction(c.prefillSuccess)&&c.prefillSuccess(l,n,c),l.trigger("cachesync",l,n,c),l.trigger("sync",l,n,c),c.prefill?s.notify(l):(_.isFunction(c.success)&&c.success(l,n,c),s.resolve(l))}c=_.defaults(c||{},{parse:!0});var a=Backbone.fetchCache.getCacheKey(this,c),h=Backbone.fetchCache._cache[a],r=!1,n=!1,s=new e.Deferred,l=this;return h&&(r=h.expires,r=r&&h.expires<(new Date).getTime(),n=h.value),r||!c.cache&&!c.prefill||!n||(null==c.async&&(c.async=!0),c.async?i(t):t(),c.prefill)?(o.modelFetch.apply(this,arguments).done(_.bind(s.resolve,this,this)).done(_.bind(Backbone.fetchCache.setCache,null,this,c)).fail(_.bind(s.reject,this,this)),s.promise()):s.promise()},Backbone.Model.prototype.sync=function(e,c){if("read"===e)return o.modelSync.apply(this,arguments);var t,h,r=c.collection,i=[];for(i.push(Backbone.fetchCache.getCacheKey(c)),r&&i.push(Backbone.fetchCache.getCacheKey(r)),t=0,h=i.length;h>t;t++)a(i[t]);return o.modelSync.apply(this,arguments)},Backbone.Collection.prototype.fetch=function(c){function t(){l[c.reset?"reset":"set"](n,c),_.isFunction(c.prefillSuccess)&&c.prefillSuccess(l),l.trigger("cachesync",l,n,c),l.trigger("sync",l,n,c),c.prefill?s.notify(l):(_.isFunction(c.success)&&c.success(l,n,c),s.resolve(l))}c=_.defaults(c||{},{parse:!0});var a=Backbone.fetchCache.getCacheKey(this,c),h=Backbone.fetchCache._cache[a],r=!1,n=!1,s=new e.Deferred,l=this;return h&&(r=h.expires,r=r&&h.expires<(new Date).getTime(),n=h.value),r||!c.cache&&!c.prefill||!n||(null==c.async&&(c.async=!0),c.async?i(t):t(),c.prefill)?(o.collectionFetch.apply(this,arguments).done(_.bind(s.resolve,this,this)).done(_.bind(Backbone.fetchCache.setCache,null,this,c)).fail(_.bind(s.reject,this,this)),s.promise()):s.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});