Permalink
Browse files

on saving the value is cloned

  • Loading branch information...
1 parent c06b4cb commit a84c1aa2900b7cf2126729a71e2f8d461177f2ec andris9 committed Nov 23, 2011
Showing with 15 additions and 10 deletions.
  1. +6 −1 jstorage.js
  2. +8 −8 jstorage.min.js
  3. +1 −1 test.html
View
7 jstorage.js
@@ -311,7 +311,7 @@
$.jStorage = {
/* Version number */
- version: "0.1.6.0",
+ version: "0.1.6.1",
/**
* Sets a key's value.
@@ -326,6 +326,11 @@
_checkKey(key);
if(_XMLService.isXML(value)){
value = {_is_xml:true,xml:_XMLService.encode(value)};
+ }else if(typeof value == "function"){
+ value = null; // functions can't be saved!
+ }else if(value && typeof value == "object"){
+ // clone the object before saving to _storage tree
+ value = json_decode(json_encode(value));
}
_storage[key] = value;
_save();
View
16 jstorage.min.js
@@ -1,8 +1,8 @@
-(function(g){function m(){if(e.jStorage)try{b=o(String(e.jStorage))}catch(a){e.jStorage="{}"}else e.jStorage="{}";j=e.jStorage?String(e.jStorage).length:0}function h(){try{e.jStorage=p(b),c&&(c.setAttribute("jStorage",e.jStorage),c.save("jStorage")),j=e.jStorage?String(e.jStorage).length:0}catch(a){}}function i(a){if(!a||typeof a!="string"&&typeof a!="number")throw new TypeError("Key name must be string or numeric");if(a=="__jstorage_meta")throw new TypeError("Reserved key name");return true}function k(){var a,
-d,c,e=Infinity,f=false;clearTimeout(n);if(b.__jstorage_meta&&typeof b.__jstorage_meta.TTL=="object"){a=+new Date;c=b.__jstorage_meta.TTL;for(d in c)c.hasOwnProperty(d)&&(c[d]<=a?(delete c[d],delete b[d],f=true):c[d]<e&&(e=c[d]));e!=Infinity&&(n=setTimeout(k,e-a));f&&h()}}if(!g||!g.toJSON&&!Object.toJSON&&!window.JSON)throw Error("jQuery, MooTools or Prototype needs to be loaded before jStorage!");var b={},e={jStorage:"{}"},c=null,j=0,p=g.toJSON||Object.toJSON||window.JSON&&(JSON.encode||JSON.stringify),
-o=g.evalJSON||window.JSON&&(JSON.decode||JSON.parse)||function(a){return String(a).evalJSON()},f=false,n,l={isXML:function(a){return(a=(a?a.ownerDocument||a:0).documentElement)?a.nodeName!=="HTML":false},encode:function(a){if(!this.isXML(a))return false;try{return(new XMLSerializer).serializeToString(a)}catch(d){try{return a.xml}catch(b){}}return false},decode:function(a){var b="DOMParser"in window&&(new DOMParser).parseFromString||window.ActiveXObject&&function(a){var b=new ActiveXObject("Microsoft.XMLDOM");
-b.async="false";b.loadXML(a);return b};if(!b)return false;a=b.call("DOMParser"in window&&new DOMParser||window,a,"text/xml");return this.isXML(a)?a:false}};g.jStorage={version:"0.1.6.0",set:function(a,d){i(a);l.isXML(d)&&(d={_is_xml:true,xml:l.encode(d)});b[a]=d;h();return d},get:function(a,d){i(a);return a in b?b[a]&&typeof b[a]=="object"&&b[a]._is_xml&&b[a]._is_xml?l.decode(b[a].xml):b[a]:typeof d=="undefined"?null:d},deleteKey:function(a){i(a);return a in b?(delete b[a],b.__jstorage_meta&&typeof b.__jstorage_meta.TTL==
-"object"&&a in b.__jstorage_meta.TTL&&delete b.__jstorage_meta.TTL[a],h(),true):false},setTTL:function(a,d){var c=+new Date;i(a);d=Number(d)||0;if(a in b){if(!b.__jstorage_meta)b.__jstorage_meta={};if(!b.__jstorage_meta.TTL)b.__jstorage_meta.TTL={};d>0?b.__jstorage_meta.TTL[a]=c+d:delete b.__jstorage_meta.TTL[a];h();k();return true}return false},flush:function(){b={};h();return true},storageObj:function(){function a(){}a.prototype=b;return new a},index:function(){var a=[],c;for(c in b)b.hasOwnProperty(c)&&
-c!="__jstorage_meta"&&a.push(c);return a},storageSize:function(){return j},currentBackend:function(){return f},storageAvailable:function(){return!!f},reInit:function(){var a;if(c&&c.addBehavior){a=document.createElement("link");c.parentNode.replaceChild(a,c);c=a;c.style.behavior="url(#default#userData)";document.getElementsByTagName("head")[0].appendChild(c);c.load("jStorage");a="{}";try{a=c.getAttribute("jStorage")}catch(b){}e.jStorage=a;f="userDataBehavior"}m()}};(function(){var a=false;if("localStorage"in
-window)try{window.localStorage.setItem("_tmptest","tmpval"),a=true,window.localStorage.removeItem("_tmptest")}catch(b){}if(a)try{if(window.localStorage)e=window.localStorage,f="localStorage"}catch(g){}else if("globalStorage"in window)try{window.globalStorage&&(e=window.globalStorage[window.location.hostname],f="globalStorage")}catch(h){}else if(c=document.createElement("link"),c.addBehavior){c.style.behavior="url(#default#userData)";document.getElementsByTagName("head")[0].appendChild(c);c.load("jStorage");
-a="{}";try{a=c.getAttribute("jStorage")}catch(i){}e.jStorage=a;f="userDataBehavior"}else{c=null;return}m();k()})()})(window.jQuery||window.$);
+(function(g){function m(){if(e.jStorage)try{c=n(""+e.jStorage)}catch(a){e.jStorage="{}"}else e.jStorage="{}";j=e.jStorage?(""+e.jStorage).length:0}function h(){try{e.jStorage=o(c),d&&(d.setAttribute("jStorage",e.jStorage),d.save("jStorage")),j=e.jStorage?(""+e.jStorage).length:0}catch(a){}}function i(a){if(!a||"string"!=typeof a&&"number"!=typeof a)throw new TypeError("Key name must be string or numeric");if("__jstorage_meta"==a)throw new TypeError("Reserved key name");return!0}function k(){var a,
+b,d,e=Infinity,f=!1;clearTimeout(p);if(c.__jstorage_meta&&"object"==typeof c.__jstorage_meta.TTL){a=+new Date;d=c.__jstorage_meta.TTL;for(b in d)d.hasOwnProperty(b)&&(d[b]<=a?(delete d[b],delete c[b],f=!0):d[b]<e&&(e=d[b]));Infinity!=e&&(p=setTimeout(k,e-a));f&&h()}}if(!g||!g.toJSON&&!Object.toJSON&&!window.JSON)throw Error("jQuery, MooTools or Prototype needs to be loaded before jStorage!");var c={},e={jStorage:"{}"},d=null,j=0,o=g.toJSON||Object.toJSON||window.JSON&&(JSON.encode||JSON.stringify),
+n=g.evalJSON||window.JSON&&(JSON.decode||JSON.parse)||function(a){return(""+a).evalJSON()},f=!1,p,l={isXML:function(a){return(a=(a?a.ownerDocument||a:0).documentElement)?"HTML"!==a.nodeName:!1},encode:function(a){if(!this.isXML(a))return!1;try{return(new XMLSerializer).serializeToString(a)}catch(b){try{return a.xml}catch(c){}}return!1},decode:function(a){var b="DOMParser"in window&&(new DOMParser).parseFromString||window.ActiveXObject&&function(a){var b=new ActiveXObject("Microsoft.XMLDOM");b.async=
+"false";b.loadXML(a);return b};if(!b)return!1;a=b.call("DOMParser"in window&&new DOMParser||window,a,"text/xml");return this.isXML(a)?a:!1}};g.jStorage={version:"0.1.6.1",set:function(a,b){i(a);if(l.isXML(b))b={_is_xml:!0,xml:l.encode(b)};else{if("function"==typeof b)return;b&&"object"==typeof b&&(b=n(o(b)))}c[a]=b;h();return b},get:function(a,b){i(a);return a in c?c[a]&&"object"==typeof c[a]&&c[a]._is_xml&&c[a]._is_xml?l.decode(c[a].xml):c[a]:"undefined"==typeof b?null:b},deleteKey:function(a){i(a);
+return a in c?(delete c[a],c.__jstorage_meta&&"object"==typeof c.__jstorage_meta.TTL&&a in c.__jstorage_meta.TTL&&delete c.__jstorage_meta.TTL[a],h(),!0):!1},setTTL:function(a,b){var d=+new Date;i(a);b=Number(b)||0;if(a in c){if(!c.__jstorage_meta)c.__jstorage_meta={};if(!c.__jstorage_meta.TTL)c.__jstorage_meta.TTL={};0<b?c.__jstorage_meta.TTL[a]=d+b:delete c.__jstorage_meta.TTL[a];h();k();return!0}return!1},flush:function(){c={};h();return!0},storageObj:function(){function a(){}a.prototype=c;return new a},
+index:function(){var a=[],b;for(b in c)c.hasOwnProperty(b)&&"__jstorage_meta"!=b&&a.push(b);return a},storageSize:function(){return j},currentBackend:function(){return f},storageAvailable:function(){return!!f},reInit:function(){var a;if(d&&d.addBehavior){a=document.createElement("link");d.parentNode.replaceChild(a,d);d=a;d.style.behavior="url(#default#userData)";document.getElementsByTagName("head")[0].appendChild(d);d.load("jStorage");a="{}";try{a=d.getAttribute("jStorage")}catch(b){}e.jStorage=
+a;f="userDataBehavior"}m()}};(function(){var a=!1;if("localStorage"in window)try{window.localStorage.setItem("_tmptest","tmpval"),a=!0,window.localStorage.removeItem("_tmptest")}catch(b){}if(a)try{if(window.localStorage)e=window.localStorage,f="localStorage"}catch(c){}else if("globalStorage"in window)try{window.globalStorage&&(e=window.globalStorage[window.location.hostname],f="globalStorage")}catch(g){}else if(d=document.createElement("link"),d.addBehavior){d.style.behavior="url(#default#userData)";
+document.getElementsByTagName("head")[0].appendChild(d);d.load("jStorage");a="{}";try{a=d.getAttribute("jStorage")}catch(h){}e.jStorage=a;f="userDataBehavior"}else{d=null;return}m();k()})()})(window.jQuery||window.$);
View
2 test.html
@@ -14,7 +14,7 @@
</style>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script>
- <script src="jstorage.min.js"></script>
+ <script src="jstorage.js"></script>
<script>
JSOTRAGE_TEST = {

0 comments on commit a84c1aa

Please sign in to comment.