yckart / jquery.storage.js

The client-side storage for every browser, on any device.
http://yckart.github.com/jquery.storage.js/
49 stars 12 forks source link

Works wrong In cookie fallback. If once removeItem is called #5

Open mzahidriaz opened 10 years ago

mzahidriaz commented 10 years ago

I've found that if localstorage is not available and this code falls back to cookie solution. in that case it always goes wrong. here it is how it goes wrong.

If used once like

$.localStorage('fbloginresult', null);

This will set the cookie expires option to -1. From going forward it always set to -1

File Code

this.setItem = function( key, value ) {
                value = JSON.stringify(value);
                return $.support[method] ? window[method].setItem(key, value) : $.cookie(options.cookiePrefix + key, value, options.cookieOptions);
            };

            this.removeItem = function( key ) {
                return $.support[method] ? window[method].removeItem(key) : $.cookie(options.cookiePrefix + key, null, $.extend(options.cookieOptions, {
                    expires: -1
                }));
            };

if (typeof key !== "undefined") {
                return typeof value !== "undefined" ? ( value === null ? this.removeItem(key) : this.setItem(key, value) ) : this.getItem(key);
            }

You see that this.removeItem is extending the cookieOptions and changing the original options passed to the calling method. Hence when it calls the this.setItem after this.removeItem the cookie options are now changed and always passed expiry as -1

So this.removeItem should be like this.

this.removeItem = function( key ) {
                return $.support[method] ? window[method].removeItem(key) : $.cookie(options.cookiePrefix + key, null, $.extend({}, options.cookieOptions, {
                    expires: -1
                }));
            };

So that it should not extend the original options passed to this function.