backbone.localcache
Advanced tools
@@ -40,3 +40,3 @@ /*! | ||
| } else { | ||
| this._createCache(); | ||
| this._createCache(attrs, options); | ||
| } | ||
@@ -84,13 +84,14 @@ | ||
| getCacheOptions: function () { | ||
| return _.extend(this.getCacheDefaults(), this.cacheOptions || {}); | ||
| getCacheOptions: function (attrs, options) { | ||
| var defaults = this.getCacheDefaults(attrs, options); | ||
| return _.extend(defaults, this.cacheOptions || {}); | ||
| }, | ||
| getCacheDefaults: function () { | ||
| getCacheDefaults: function (attrs, options) { | ||
| return { | ||
| bucket: this.getBucketName() | ||
| bucket: this.getBucketName(attrs, options) | ||
| }; | ||
| }, | ||
| getBucketName: function () { | ||
| getBucketName: function (attrs, options) { | ||
| return this.cacheName; | ||
@@ -133,4 +134,4 @@ }, | ||
| _createCache: function () { | ||
| this.cache = new Cache(this.getCacheOptions()); | ||
| _createCache: function (attrs, options) { | ||
| this.cache = new Cache(this.getCacheOptions(attrs, options)); | ||
| this.cache.flushExpired(); | ||
@@ -137,0 +138,0 @@ |
@@ -473,3 +473,3 @@ (function (root, factory) { | ||
| } else { | ||
| this._createCache(); | ||
| this._createCache(attrs, options); | ||
| } | ||
@@ -506,9 +506,10 @@ // retrieve attrs (specifically uuid) prior to adding attachments | ||
| * -------------------------------------------------------------------------*/ | ||
| getCacheOptions: function () { | ||
| return _.extend(this.getCacheDefaults(), this.cacheOptions || {}); | ||
| getCacheOptions: function (attrs, options) { | ||
| var defaults = this.getCacheDefaults(attrs, options); | ||
| return _.extend(defaults, this.cacheOptions || {}); | ||
| }, | ||
| getCacheDefaults: function () { | ||
| return { bucket: this.getBucketName() }; | ||
| getCacheDefaults: function (attrs, options) { | ||
| return { bucket: this.getBucketName(attrs, options) }; | ||
| }, | ||
| getBucketName: function () { | ||
| getBucketName: function (attrs, options) { | ||
| return this.cacheName; | ||
@@ -542,4 +543,4 @@ }, | ||
| * -------------------------------------------------------------------------*/ | ||
| _createCache: function () { | ||
| this.cache = new Cache(this.getCacheOptions()); | ||
| _createCache: function (attrs, options) { | ||
| this.cache = new Cache(this.getCacheOptions(attrs, options)); | ||
| this.cache.flushExpired(); | ||
@@ -546,0 +547,0 @@ this.on('add', this._onAddToCollection, this); |
@@ -1,1 +0,1 @@ | ||
| !function(a,b){"function"==typeof define&&define.amd?define(["underscore"],function(c){return a.returnExportsGlobal=b(c)}):"object"==typeof exports?module.exports=b(require("underscore")):a.backboneLocalcache=b(a.underscore)}(this,function(a){var b,c,d,e;return b=function(a){var b=function(a,b){return Object.defineProperty(b,"__mixin",{enumerable:!1,writable:!0}),b.__mixin=a,b};return b}({}),function(a,b){e=function(){return"function"==typeof b?b():b}()}(this,function(){var a,b,c="",d=!1,e={PREFIX:"lscache-",SUFFIX:"-cacheexpiration",EXPIRY_RADIX:10,EXPIRY_UNITS:6e4,_supportsStorage:function(){var b="__lscachetest__",c=b;if(void 0!==a)return a;try{e._setItem(b,c),e._removeItem(b),a=!0}catch(d){a=!!e._isOutOfSpace(d)}return a},_isOutOfSpace:function(a){return!!(a&&"QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name)},_supportsJSON:function(){return void 0===b&&(b=null!=window.JSON),b},_escapeRegExpSpecialCharacters:function(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")},_expirationKey:function(a){return a+e.SUFFIX},_currentTime:function(){return Math.floor((new Date).getTime()/e.EXPIRY_UNITS)},_getMaxDate:function(){return e._maxDate=e._maxDate||Math.floor(864e13/e.EXPIRY_UNITS),e._maxDate},_getItem:function(a){return localStorage.getItem(e.PREFIX+c+a)},_setItem:function(a,b){localStorage.removeItem(e.PREFIX+c+a),localStorage.setItem(e.PREFIX+c+a,b)},_removeItem:function(a){localStorage.removeItem(e.PREFIX+c+a)},_eachKey:function(a){for(var b=new RegExp("^"+e.PREFIX+e._escapeRegExpSpecialCharacters(c)+"(.*)"),d=localStorage.length-1;d>=0;--d){var f=localStorage.key(d);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(e.SUFFIX)<0&&a(f,e._expirationKey(f))}},_flushItem:function(a){var b=e._expirationKey(a);e._removeItem(a),e._removeItem(b)},_flushExpiredItem:function(a){var b=e._expirationKey(a),c=e._getItem(b);if(c){var d=parseInt(c,e.EXPIRY_RADIX);if(e._currentTime()>=d)return e._removeItem(a),e._removeItem(b),!0}},_warn:function(a,b){d&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))},set:function(a,b,c){if(e._supportsStorage()){if("string"!=typeof b){if(!e._supportsJSON())return;try{b=JSON.stringify(b)}catch(d){return}}try{e._setItem(a,b)}catch(d){if(!e._isOutOfSpace(d))return void e._warn("Could not add item with key '"+a+"'",d);var f,g=[];e._eachKey(function(a,b){var c=e._getItem(b);c=c?parseInt(c,e.EXPIRY_RADIX):e._getMaxDate(),g.push({key:a,size:(e._getItem(a)||"").length,expiration:c})}),g.sort(function(a,b){return b.expiration-a.expiration});for(var h=(b||"").length;g.length&&h>0;)f=g.pop(),e._warn("Cache is full, removing item with key '"+a+"'"),e._flushItem(f.key),h-=f.size;try{e._setItem(a,b)}catch(d){return void e._warn("Could not add item with key '"+a+"', perhaps it's too big?",d)}}c?e._setItem(e._expirationKey(a),(e._currentTime()+c).toString(e.EXPIRY_RADIX)):e._removeItem(e._expirationKey(a))}},get:function(a){if(!e._supportsStorage())return null;if(e._flushExpiredItem(a))return null;var b=e._getItem(a);if(!b||!e._supportsJSON())return b;try{return JSON.parse(b)}catch(c){return b}},remove:function(a){e._supportsStorage()&&e._flushItem(a)},supported:function(){return e._supportsStorage()},flush:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushItem(a)})},flushExpired:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushExpiredItem(a)})},setBucket:function(a){c=a},resetBucket:function(){c=""},enableWarnings:function(a){d=a}};return e}),c=function(a){var b=e,c=function(a){a=a||{},this.namespace=a.namespace,this.bucket=a.bucket,this.ttl=a.ttl};return c.prototype.bucketId=function(){var a="";return this.namespace&&(a+=this.namespace+"-"),this.bucket&&(a+=this.bucket+"-"),a},c.prototype.get=function(){return b.setBucket(this.bucketId()),b.get.apply(b,arguments)},c.prototype.set=function(a,c,d){return b.setBucket(this.bucketId()),b.set.call(b,a,c,d||this.ttl)},c.prototype.remove=function(){return b.setBucket(this.bucketId()),b.remove.apply(b,arguments)},c.prototype.flush=function(){return b.setBucket(this.bucketId()),b.flush.apply(b,arguments)},c.prototype.flushExpired=function(){return b.setBucket(this.bucketId()),b.flushExpired.apply(b,arguments)},c.prototype.all=function(){var a={};return b.setBucket(this.bucketId()),b._eachKey(function(c,d){a[c]=b._getItem(c)}),a},c}({}),d=function(d){var e=a,f=b,g=c;return{attachments:["meta"],_flags:{restoredFromCache:!1},constructor:f("decorate",function(a,b,c){if(c=c||{},this.__uuid=this._getUUID(c),!this.cacheName)throw new Error("Required attribute `cacheName` missing.");this._createCache(),b=this._mergeAttrs(this._loadCacheAttrs(),b),this.__uuid=this._isModel(this)?this._getUUID(b,!0):null,this.__attachments=this._loadAttachments(c),a.call(this,b,c),this._syncCacheAttrs(),delete this.__attachments,this._syncAttachments(this.attachments),this._addAttachmentListeners(this.attachments)}),_prepareModel:f("decorate",function(a,b,c){var d=c.__uuid=this._getUUID(b,!0),e=a.call(this,b,c);return e.__uuid=d,e}),restoredFromCache:function(){return this._flags.restoredFromCache},getCacheOptions:function(){return e.extend(this.getCacheDefaults(),this.cacheOptions||{})},getCacheDefaults:function(){return{bucket:this.getBucketName()}},getBucketName:function(){return this.cacheName},_onAddToCollection:function(a,b,c){c&&c.ignoreCache||this._setCacheEntry(a)},_onRemoveFromCollection:function(a,b,c){c&&c.ignoreCache||this.cache.remove(a.id)},_onResetCollection:function(a,b){b&&b.ignoreCache||this.cache.flush()},_onChangeModel:function(a,b){b&&b.ignoreCache||this._setCacheEntry(a)},_createCache:function(){this.cache=new g(this.getCacheOptions()),this.cache.flushExpired(),this.on("add",this._onAddToCollection,this),this.on("remove",this._onRemoveFromCollection,this),this.on("reset",this._onResetCollection,this),this.on("change",this._onChangeModel,this)},_loadCacheAttrs:function(){var a=this._getCacheAttrs();return this._flags.restoredFromCache=!e.isEmpty(a),this._isModel(this)&&e.isArray(a)?a[0]:a},_getCacheAttrs:function(){return this.__uuid?this._getFromCacheByUUID():this._getFromCacheByFilter()},_getFromCacheByUUID:function(){return this.cache.get(this._getCacheKey(this))},_getFromCacheByFilter:function(){return this.attachmentKeys=e.object(this.attachments,[]),e.chain(this.cache.all()).pick(this._filterCacheAttrs.bind(this)).map(this._attachUUID.bind(this)).value()},_filterCacheAttrs:function(a,b){var c=b.lastIndexOf("__");return-1!==c?!this.attachmentKeys.hasOwnProperty(b.slice(c+2)):!0},_attachUUID:function(a,b){var c=JSON.parse(a);return c.__uuid=b,c},_syncCacheAttrs:function(){return this.models?e.each(this.models,this._setCacheEntry,this):this._setCacheEntry(this)},_setCacheEntry:function(a){var b=this._getCacheKey(a);return b?e.isEmpty(a.attributes)?this.cache.remove(b):this.cache.set(b,a.attributes):void 0},_mergeAttrs:function(a,b){return b?e.isEmpty(a)?b:this._isModel(this)?this._mergeModelAttrs(a,b):this._mergeCollectionAttrs(a,b):a},_mergeModelAttrs:function(a,b){return e.extend(a,b)},_mergeCollectionAttrs:function(a,b){var c=function(a){return a[this.modelId]}.bind(this);return e.isArray(b)&&e.isArray(a)?e.uniq(e.union(b,a),c):e.uniq([b].concat(a),c)},_loadAttachments:function(a){var b=e.reduce(this.attachments,function(b,c){return b[c]=e.extend({},this._loadAttachment(c),a[c]||{}),b},{},this);return b},_loadAttachment:function(a){var b=this._getCacheKey(this,a);return this.cache.get(b)},_syncAttachments:function(a){e.each(a,function(a){this._syncAttachment(a,this[a])},this)},_syncAttachment:function(a,b){if(b){var c=this._getCacheKey(this,a);return this.cache.set(c,b.attributes)}},_addAttachmentListeners:function(a){e.each(a,function(a){this.on(a+":change",this._onChangeAttachment.bind(this,a))},this)},_onChangeAttachment:function(a,b,c){c&&c.ignoreCache||this[a]!==b||this._syncAttachment(a,b)},_isModel:function(a){return a instanceof Backbone.Model},_getUUID:function(a,b){a=a||{};var c=a.__uuid||this.__uuid;return delete a.__uuid,!c&&b?this._generateUUID():c},_generateUUID:function(){var a=(new Date).getTime(),b="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(b){var c=(a+16*Math.random())%16|0;return a=Math.floor(a/16),("x"===b?c:3&c|8).toString(16)});return b},_getCacheKey:function(a,b){return this._isModel(a)?b?a.__uuid+"-__"+b:a.__uuid:"__"+b}}}({})}); | ||
| !function(a,b){"function"==typeof define&&define.amd?define(["underscore"],function(c){return a.returnExportsGlobal=b(c)}):"object"==typeof exports?module.exports=b(require("underscore")):a.backboneLocalcache=b(a.underscore)}(this,function(a){var b,c,d,e;return b=function(a){var b=function(a,b){return Object.defineProperty(b,"__mixin",{enumerable:!1,writable:!0}),b.__mixin=a,b};return b}({}),function(a,b){e=function(){return"function"==typeof b?b():b}()}(this,function(){var a,b,c="",d=!1,e={PREFIX:"lscache-",SUFFIX:"-cacheexpiration",EXPIRY_RADIX:10,EXPIRY_UNITS:6e4,_supportsStorage:function(){var b="__lscachetest__",c=b;if(void 0!==a)return a;try{e._setItem(b,c),e._removeItem(b),a=!0}catch(d){a=!!e._isOutOfSpace(d)}return a},_isOutOfSpace:function(a){return!!(a&&"QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name)},_supportsJSON:function(){return void 0===b&&(b=null!=window.JSON),b},_escapeRegExpSpecialCharacters:function(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")},_expirationKey:function(a){return a+e.SUFFIX},_currentTime:function(){return Math.floor((new Date).getTime()/e.EXPIRY_UNITS)},_getMaxDate:function(){return e._maxDate=e._maxDate||Math.floor(864e13/e.EXPIRY_UNITS),e._maxDate},_getItem:function(a){return localStorage.getItem(e.PREFIX+c+a)},_setItem:function(a,b){localStorage.removeItem(e.PREFIX+c+a),localStorage.setItem(e.PREFIX+c+a,b)},_removeItem:function(a){localStorage.removeItem(e.PREFIX+c+a)},_eachKey:function(a){for(var b=new RegExp("^"+e.PREFIX+e._escapeRegExpSpecialCharacters(c)+"(.*)"),d=localStorage.length-1;d>=0;--d){var f=localStorage.key(d);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(e.SUFFIX)<0&&a(f,e._expirationKey(f))}},_flushItem:function(a){var b=e._expirationKey(a);e._removeItem(a),e._removeItem(b)},_flushExpiredItem:function(a){var b=e._expirationKey(a),c=e._getItem(b);if(c){var d=parseInt(c,e.EXPIRY_RADIX);if(e._currentTime()>=d)return e._removeItem(a),e._removeItem(b),!0}},_warn:function(a,b){d&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))},set:function(a,b,c){if(e._supportsStorage()){if("string"!=typeof b){if(!e._supportsJSON())return;try{b=JSON.stringify(b)}catch(d){return}}try{e._setItem(a,b)}catch(d){if(!e._isOutOfSpace(d))return void e._warn("Could not add item with key '"+a+"'",d);var f,g=[];e._eachKey(function(a,b){var c=e._getItem(b);c=c?parseInt(c,e.EXPIRY_RADIX):e._getMaxDate(),g.push({key:a,size:(e._getItem(a)||"").length,expiration:c})}),g.sort(function(a,b){return b.expiration-a.expiration});for(var h=(b||"").length;g.length&&h>0;)f=g.pop(),e._warn("Cache is full, removing item with key '"+a+"'"),e._flushItem(f.key),h-=f.size;try{e._setItem(a,b)}catch(d){return void e._warn("Could not add item with key '"+a+"', perhaps it's too big?",d)}}c?e._setItem(e._expirationKey(a),(e._currentTime()+c).toString(e.EXPIRY_RADIX)):e._removeItem(e._expirationKey(a))}},get:function(a){if(!e._supportsStorage())return null;if(e._flushExpiredItem(a))return null;var b=e._getItem(a);if(!b||!e._supportsJSON())return b;try{return JSON.parse(b)}catch(c){return b}},remove:function(a){e._supportsStorage()&&e._flushItem(a)},supported:function(){return e._supportsStorage()},flush:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushItem(a)})},flushExpired:function(){e._supportsStorage()&&e._eachKey(function(a){e._flushExpiredItem(a)})},setBucket:function(a){c=a},resetBucket:function(){c=""},enableWarnings:function(a){d=a}};return e}),c=function(a){var b=e,c=function(a){a=a||{},this.namespace=a.namespace,this.bucket=a.bucket,this.ttl=a.ttl};return c.prototype.bucketId=function(){var a="";return this.namespace&&(a+=this.namespace+"-"),this.bucket&&(a+=this.bucket+"-"),a},c.prototype.get=function(){return b.setBucket(this.bucketId()),b.get.apply(b,arguments)},c.prototype.set=function(a,c,d){return b.setBucket(this.bucketId()),b.set.call(b,a,c,d||this.ttl)},c.prototype.remove=function(){return b.setBucket(this.bucketId()),b.remove.apply(b,arguments)},c.prototype.flush=function(){return b.setBucket(this.bucketId()),b.flush.apply(b,arguments)},c.prototype.flushExpired=function(){return b.setBucket(this.bucketId()),b.flushExpired.apply(b,arguments)},c.prototype.all=function(){var a={};return b.setBucket(this.bucketId()),b._eachKey(function(c,d){a[c]=b._getItem(c)}),a},c}({}),d=function(d){var e=a,f=b,g=c;return{attachments:["meta"],_flags:{restoredFromCache:!1},constructor:f("decorate",function(a,b,c){if(c=c||{},this.__uuid=this._getUUID(c),!this.cacheName)throw new Error("Required attribute `cacheName` missing.");this._createCache(b,c),b=this._mergeAttrs(this._loadCacheAttrs(),b),this.__uuid=this._isModel(this)?this._getUUID(b,!0):null,this.__attachments=this._loadAttachments(c),a.call(this,b,c),this._syncCacheAttrs(),delete this.__attachments,this._syncAttachments(this.attachments),this._addAttachmentListeners(this.attachments)}),_prepareModel:f("decorate",function(a,b,c){var d=c.__uuid=this._getUUID(b,!0),e=a.call(this,b,c);return e.__uuid=d,e}),restoredFromCache:function(){return this._flags.restoredFromCache},getCacheOptions:function(a,b){var c=this.getCacheDefaults(a,b);return e.extend(c,this.cacheOptions||{})},getCacheDefaults:function(a,b){return{bucket:this.getBucketName(a,b)}},getBucketName:function(a,b){return this.cacheName},_onAddToCollection:function(a,b,c){c&&c.ignoreCache||this._setCacheEntry(a)},_onRemoveFromCollection:function(a,b,c){c&&c.ignoreCache||this.cache.remove(a.id)},_onResetCollection:function(a,b){b&&b.ignoreCache||this.cache.flush()},_onChangeModel:function(a,b){b&&b.ignoreCache||this._setCacheEntry(a)},_createCache:function(a,b){this.cache=new g(this.getCacheOptions(a,b)),this.cache.flushExpired(),this.on("add",this._onAddToCollection,this),this.on("remove",this._onRemoveFromCollection,this),this.on("reset",this._onResetCollection,this),this.on("change",this._onChangeModel,this)},_loadCacheAttrs:function(){var a=this._getCacheAttrs();return this._flags.restoredFromCache=!e.isEmpty(a),this._isModel(this)&&e.isArray(a)?a[0]:a},_getCacheAttrs:function(){return this.__uuid?this._getFromCacheByUUID():this._getFromCacheByFilter()},_getFromCacheByUUID:function(){return this.cache.get(this._getCacheKey(this))},_getFromCacheByFilter:function(){return this.attachmentKeys=e.object(this.attachments,[]),e.chain(this.cache.all()).pick(this._filterCacheAttrs.bind(this)).map(this._attachUUID.bind(this)).value()},_filterCacheAttrs:function(a,b){var c=b.lastIndexOf("__");return-1!==c?!this.attachmentKeys.hasOwnProperty(b.slice(c+2)):!0},_attachUUID:function(a,b){var c=JSON.parse(a);return c.__uuid=b,c},_syncCacheAttrs:function(){return this.models?e.each(this.models,this._setCacheEntry,this):this._setCacheEntry(this)},_setCacheEntry:function(a){var b=this._getCacheKey(a);return b?e.isEmpty(a.attributes)?this.cache.remove(b):this.cache.set(b,a.attributes):void 0},_mergeAttrs:function(a,b){return b?e.isEmpty(a)?b:this._isModel(this)?this._mergeModelAttrs(a,b):this._mergeCollectionAttrs(a,b):a},_mergeModelAttrs:function(a,b){return e.extend(a,b)},_mergeCollectionAttrs:function(a,b){var c=function(a){return a[this.modelId]}.bind(this);return e.isArray(b)&&e.isArray(a)?e.uniq(e.union(b,a),c):e.uniq([b].concat(a),c)},_loadAttachments:function(a){var b=e.reduce(this.attachments,function(b,c){return b[c]=e.extend({},this._loadAttachment(c),a[c]||{}),b},{},this);return b},_loadAttachment:function(a){var b=this._getCacheKey(this,a);return this.cache.get(b)},_syncAttachments:function(a){e.each(a,function(a){this._syncAttachment(a,this[a])},this)},_syncAttachment:function(a,b){if(b){var c=this._getCacheKey(this,a);return this.cache.set(c,b.attributes)}},_addAttachmentListeners:function(a){e.each(a,function(a){this.on(a+":change",this._onChangeAttachment.bind(this,a))},this)},_onChangeAttachment:function(a,b,c){c&&c.ignoreCache||this[a]!==b||this._syncAttachment(a,b)},_isModel:function(a){return a instanceof Backbone.Model},_getUUID:function(a,b){a=a||{};var c=a.__uuid||this.__uuid;return delete a.__uuid,!c&&b?this._generateUUID():c},_generateUUID:function(){var a=(new Date).getTime(),b="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(b){var c=(a+16*Math.random())%16|0;return a=Math.floor(a/16),("x"===b?c:3&c|8).toString(16)});return b},_getCacheKey:function(a,b){return this._isModel(a)?b?a.__uuid+"-__"+b:a.__uuid:"__"+b}}}({})}); |
@@ -40,3 +40,3 @@ /*! | ||
| } else { | ||
| this._createCache(); | ||
| this._createCache(attrs, options); | ||
| } | ||
@@ -84,13 +84,14 @@ | ||
| getCacheOptions: function () { | ||
| return _.extend(this.getCacheDefaults(), this.cacheOptions || {}); | ||
| getCacheOptions: function (attrs, options) { | ||
| var defaults = this.getCacheDefaults(attrs, options); | ||
| return _.extend(defaults, this.cacheOptions || {}); | ||
| }, | ||
| getCacheDefaults: function () { | ||
| getCacheDefaults: function (attrs, options) { | ||
| return { | ||
| bucket: this.getBucketName() | ||
| bucket: this.getBucketName(attrs, options) | ||
| }; | ||
| }, | ||
| getBucketName: function () { | ||
| getBucketName: function (attrs, options) { | ||
| return this.cacheName; | ||
@@ -133,4 +134,4 @@ }, | ||
| _createCache: function () { | ||
| this.cache = new Cache(this.getCacheOptions()); | ||
| _createCache: function (attrs, options) { | ||
| this.cache = new Cache(this.getCacheOptions(attrs, options)); | ||
| this.cache.flushExpired(); | ||
@@ -137,0 +138,0 @@ |
+1
-1
@@ -5,3 +5,3 @@ { | ||
| "author": "Jarid Margolin <jarid@firstopinionapp.com>", | ||
| "version": "2.0.1", | ||
| "version": "2.1.0", | ||
| "homepage": "https://github.com/firstopinion/backbone.localcache", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -40,3 +40,3 @@ /*! | ||
| } else { | ||
| this._createCache(); | ||
| this._createCache(attrs, options); | ||
| } | ||
@@ -84,13 +84,14 @@ | ||
| getCacheOptions: function () { | ||
| return _.extend(this.getCacheDefaults(), this.cacheOptions || {}); | ||
| getCacheOptions: function (attrs, options) { | ||
| var defaults = this.getCacheDefaults(attrs, options); | ||
| return _.extend(defaults, this.cacheOptions || {}); | ||
| }, | ||
| getCacheDefaults: function () { | ||
| getCacheDefaults: function (attrs, options) { | ||
| return { | ||
| bucket: this.getBucketName() | ||
| bucket: this.getBucketName(attrs, options) | ||
| }; | ||
| }, | ||
| getBucketName: function () { | ||
| getBucketName: function (attrs, options) { | ||
| return this.cacheName; | ||
@@ -133,4 +134,4 @@ }, | ||
| _createCache: function () { | ||
| this.cache = new Cache(this.getCacheOptions()); | ||
| _createCache: function (attrs, options) { | ||
| this.cache = new Cache(this.getCacheOptions(attrs, options)); | ||
| this.cache.flushExpired(); | ||
@@ -137,0 +138,0 @@ |
@@ -14,2 +14,3 @@ /*! | ||
| var assert = require('proclaim'); | ||
| var sinon = require('sinon'); | ||
| var Backbone = require('backbone'); | ||
@@ -94,2 +95,27 @@ var mixins = require('mixins/mixins'); | ||
| describe('cache configuration', function () { | ||
| beforeEach(function () { | ||
| this.defaultsSpy = sinon.spy(CachedModel.prototype, 'getCacheDefaults'); | ||
| this.bucketSpy = sinon.spy(CachedModel.prototype, 'getBucketName'); | ||
| this.options = {}; | ||
| this.model = new CachedModel(attrs1, this.options); | ||
| }); | ||
| afterEach(function () { | ||
| this.defaultsSpy.restore(); | ||
| this.bucketSpy.restore(); | ||
| }); | ||
| it('Should pass attrs and options to getCacheDefaults.', function () { | ||
| assert.isTrue(this.defaultsSpy.calledWith(attrs1, this.options)); | ||
| }); | ||
| it('Should pass attrs and options to getBucketName.', function () { | ||
| assert.isTrue(this.bucketSpy.calledWith(attrs1, this.options)); | ||
| }); | ||
| }); | ||
| describe('model', function () { | ||
@@ -96,0 +122,0 @@ |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
78926
1.77%1736
1.34%