forked from mikeedwards/Backbone.DOMStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.domStorage-min.js
4 lines (4 loc) · 3.78 KB
/
backbone.domStorage-min.js
1
2
3
4
/**
* Backbone localStorage and sessionStorage Adapter
* https://github.com/mikeedwards/Backbone.DOMStorage
*/(function(){function n(){return((1+Math.random())*65536|0).toString(16).substring(1)}function r(){return n()+n()+"-"+n()+"-"+n()+"-"+n()+"-"+n()+n()+n()}var e=this._,t=this.Backbone;t.LocalStorage=window.Store=function(e){this.name=e;var t=this.localStorage().getItem(this.name)||"";this.records=t&&t.split(",")||[]},e.extend(t.LocalStorage.prototype,{save:function(){this.localStorage().setItem(this.name,this.records.join(","))},safeGet:function(e){var t=this.localStorage().getItem(e);return t?t:"{}"},create:function(e){return e.id||(e.id=r(),e.set(e.idAttribute,e.id)),this.localStorage().setItem(this.name+"-"+e.id,JSON.stringify(e)),this.records.push(e.id.toString()),this.save(),e.toJSON()},update:function(t){return this.localStorage().setItem(this.name+"-"+t.id,JSON.stringify(t)),e.include(this.records,t.id.toString())||this.records.push(t.id.toString()),this.save(),t.toJSON()},find:function(e){return JSON.parse(this.safeGet(this.name+"-"+e.id))},findAll:function(){return e(this.records).chain().map(function(t){var n=JSON.parse(this.safeGet(this.name+"-"+t));return e.isEmpty(n)?!1:n},this).compact().value()},destroy:function(t){return this.localStorage().removeItem(this.name+"-"+t.id),this.records=e.reject(this.records,function(e){return e==t.id.toString()}),this.save(),t},localStorage:function(){return localStorage}}),t.LocalStorage.sync=window.Store.sync=t.localSync=function(e,t,n,r){var i=t.localStorage||t.collection.localStorage,s,r="Record not found",o=$.Deferred&&$.Deferred();typeof n=="function"&&(n={success:n,error:r});try{switch(e){case"read":s=t.id!=undefined?i.find(t):i.findAll();break;case"create":s=i.create(t);break;case"update":s=i.update(t);break;case"delete":s=i.destroy(t)}}catch(u){r=u}return s?(n.success(s),o&&o.resolve()):(n.error(r),o&&o.reject()),o&&o.promise()},t.SessionStorage=window.SessionStore=function(e){this.name=e;var t=this.sessionStorage().getItem(this.name)||"";this.records=t&&t.split(",")||[]},e.extend(t.SessionStorage.prototype,{save:function(){this.sessionStorage().setItem(this.name,this.records.join(","))},safeGet:function(e){var t=this.sessionStorage().getItem(e);return t?t:"{}"},create:function(e){return e.id||(e.id=r(),e.set(e.idAttribute,e.id)),this.sessionStorage().setItem(this.name+"-"+e.id,JSON.stringify(e)),this.records.push(e.id.toString()),this.save(),e.toJSON()},update:function(t){return this.sessionStorage().setItem(this.name+"-"+t.id,JSON.stringify(t)),e.include(this.records,t.id.toString())||this.records.push(t.id.toString()),this.save(),t.toJSON()},find:function(e){return JSON.parse(this.safeGet(this.name+"-"+e.id))},findAll:function(){return e(this.records).chain().map(function(t){var n=JSON.parse(this.safeGet(this.name+"-"+t));return e.isEmpty(n)?!1:n},this).compact().value()},destroy:function(t){return this.sessionStorage().removeItem(this.name+"-"+t.id),this.records=e.reject(this.records,function(e){return e==t.id.toString()}),this.save(),t},sessionStorage:function(){return sessionStorage}}),t.SessionStorage.sync=t.sessionSync=function(e,t,n,r){var i=t.sessionStorage||t.collection.sessionStorage,s,r="Record not found",o=$.Deferred&&$.Deferred();typeof n=="function"&&(n={success:n,error:r});try{switch(e){case"read":s=t.id!=undefined?i.find(t):i.findAll();break;case"create":s=i.create(t);break;case"update":s=i.update(t);break;case"delete":s=i.destroy(t)}}catch(u){r=u}return s?(n.success(s),o&&o.resolve()):(n.error(r),o&&o.reject()),o&&o.promise()},t.ajaxSync=t.sync,t.getSyncMethod=function(e){return e.localStorage||e.collection&&e.collection.localStorage?t.localSync:e.sessionStorage||e.collection&&e.collection.sessionStorage?t.SessionStorage.sync:t.ajaxSync},t.sync=function(e,n,r,i){return t.getSyncMethod(n).apply(this,[e,n,r,i])}})();