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;
}
}
})
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;
}
}
})