PDA

View Full Version : [FIXED][3.0.0] Ext.state.StateManager#set/get and empty arrays / empty objects



kleins
6 Jul 2009, 4:29 AM
DESCRIPTION
If you save an empty object using StateManager#set and CookieProvider and retrieve the same object, you get back {"" : undefined} instead of {}. The same holds for empty arrays where you get back [""] instead of [].

TESTCASE
The following code fails with 3.0RC2



// create cookies for testing
if (Ext.state.Manager.get('empty-object') == null) {
Ext.state.Manager.set('empty-object', {});
Ext.state.Manager.set('empty-array', []);
location.reload();
};

var loadedObject = Ext.state.Manager.get('empty-object');
var ok = true;
for (key in loadedObject) {
ok = false;
}
console.log('empty object is loaded properly: ' + ok);

var loadedArray = Ext.state.Manager.get('empty-array');
loadedArray = Ext.state.Manager.get('empty-array');
console.log('empty array is loaded properly: ' + (loadedArray.length == 0));



SOLUTION
In Ext.state.Provider#decodeValue check for empty values in the appropriate places. Here is my patch


Ext.override(Ext.state.Provider, {
decodeValue : function(cookie){
var re = /^(a|n|d|b|s|o)\:(.*)$/;
var matches = re.exec(unescape(cookie));
if(!matches || !matches[1]) return; // non state cookie
var type = matches[1];
var v = matches[2];
switch(type){
case "n":
return parseFloat(v);
case "d":
return new Date(Date.parse(v));
case "b":
return (v == "1");
case "a":
var all = [];
if (v != "") {
var values = v.split("^");
for (var i = 0, len = values.length; i < len; i++) {
all.push(this.decodeValue(values[i]));
}
}
return all;
case "o":
var all = {};
if (v != "") {
var values = v.split("^");
for (var i = 0, len = values.length; i < len; i++) {
var kv = values[i].split("=");
all[kv[0]] = this.decodeValue(kv[1]);
}
}
return all;
default:
return v;
}
}
})

mystix
6 Jul 2009, 4:35 AM
you might want to highlight your changes in red for clarity.
additionally, you might want to retest against RC3 instead of RC2.

kleins
6 Jul 2009, 4:42 AM
Thanks.
The changes are in red now.
I don't have the time to test against RC3, I just took a look at the source, though, and the code for decodeValue hasn't changed, so the problem presumably still exists.

mystix
6 Jul 2009, 4:49 AM
Thanks.
The changes are in red now.
I don't have the time to test against RC3, I just took a look at the source, though, and the code for decodeValue hasn't changed, so the problem presumably still exists.

righto. i've updated the thread title to match.

wiznia
21 Jul 2009, 4:50 AM
Yes, this is indeed a bug and it's on the stable version of Ext 3. Should be commited to patch next versions...

evant
21 Jul 2009, 8:52 PM
Fixed in SVN.