andris9/jStorage — Issue #20: Need to clone objects before storing in storage object
Author: odedbd
Created: 2011-11-17 17:28:52+00:00
State: open

Hi,

First let me thank you for the wonderful little tool you've created, very useful!

I have run into a minor issue, which can be easily fixed (I'm no git user, so I didn't create a pull req for this).

in set:

set: function(key, value){
_checkKey(key);
if(_XMLService.isXML(value)){
value = {_is_xml:true,xml:_XMLService.encode(value)};
}
_storage[key] = value;
_save();
return value;
}

If the value is an object, and it is later changed, the _storage cache will reflect those changes (while the localStorage won't). This is an issue, since the stored object may (and in my application does) acquire non-json-serializable properties (such as jQuery wrapped elements). when ever some other key is changed and the entire _storage object needs to be serialized, things break.

my fix was quite simple, perhaps there's a better way to go about this-

set: function(key, value){
var clone;
_checkKey(key);

if(typeof(value) === 'object'){
clone = $.extend(true, {}, value);
}else{
clone=value;
}

if(_XMLService.isXML(clone)){
clone= {_is_xml:true,xml:_XMLService.encode(clone)};
}
_storage[key] = clone;
_save();
return clone;
}

Please note that this uses jQuery's extend function explicitly, I have no idea whether this could roll with other libraries. This works fine for me, so I hope it might help others as well.

Thanks again,
o