PDA

View Full Version : [2.0b1][SOLVED] Grid Column Width State Restoration (provider encoding issue)



ambience
12 Oct 2007, 2:11 PM
I was happy to find out that Grids automagicly stored and restored alot of their configuration information. Unfortunately, when I had a grid inside of a tab (nested in some other stuff) it would render incorrectly on page refresh. While the headers them selves appered to be of the correctly restored width, the table cells where not rendered correctly. Cells of columns I had not resized in the previous page load would not have their width set, and as a result would skew any cells after them. After a little bit of poking around in firebug, I *think* I narrowed it down to an encoding problem. The stored column information looked a little like this:


state: {
columns: [
{id: 1, width: undefined}, ...
]
}

But on the return trip, the restored state information looked like this:


state: {
columns: [
{id: 1, width: 'undefined'}, ...
]
}


By 'patching' Ext.state.Provider.encodeValue to ignore undefined values in the encode. I was able to fix the restoration issue.



Ext.state.Provider.prototype.encodeValue = function(v){
var enc;
if(typeof v == "number"){
enc = "n:" + v;
}else if(typeof v == "boolean"){
enc = "b:" + (v ? "1" : "0");
}else if(v instanceof Date){
enc = "d:" + v.toGMTString();
}else if(v instanceof Array){
var flat = "";
for(var i = 0, len = v.length; i < len; i++){
flat += this.encodeValue(v[i]);
if(i != len-1) flat += "^";
}
enc = "a:" + flat;
}else if(typeof v == "object"){
var flat = "";
for(var key in v){
if(typeof v[key] != "function" && v[key] !== undefined){
flat += key + "=" + this.encodeValue(v[key]) + "^";
}
}
enc = "o:" + flat.substring(0, flat.length-1);
}else{
enc = "s:" + v;
}
return escape(enc);
};


As a side note, the addition of 'beforestaterestore', 'staterestored', 'beforestatesave', and 'statesaved' events would be very helpful for plugins that would like to piggyback some additional state information.

jack.slocum
12 Oct 2007, 8:15 PM
Thanks. I will add that in.

jack.slocum
12 Oct 2007, 8:22 PM
The events have been added as well. Nice suggestion. :)

DragonFist
28 Oct 2007, 6:53 PM
A problem that I discovered that may be a side effect of this handling is that the layout config property "collapseMode" defaults to "undefined" as a string.

After state is saved for collapse, I sometimes get a "mini" collapseMode as a result.

probably be solved but setting the "undefined" setting to something like "default" instead.

ambience
28 Oct 2007, 8:23 PM
v[key] !== undefined should not match v[key] values that are "undefined" as the second '=' is instructing javascript to do a type sensitive comparison. Just FYI =)

marmstrong
15 Nov 2007, 5:14 AM
Which release is this fix in?

aconran
15 Nov 2007, 8:34 AM
The check for undefined is in RC1.

purestgreen
11 Nov 2008, 3:48 AM
Hi there,
I'm having trouble using the beforestaterestore event - it fires during initialisation, before I've had a chance to add a listener. Is there a way I can hook into the initial state restoration event?

Thanks,
Simon

Condor
11 Nov 2008, 5:36 AM
Define your beforestaterestore event handler in the listeners config option instead of using addListener()/on() after the component is created.