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