PDA

View Full Version : Ext.form.Action handleResponse check responseText



mdissel
12 Jul 2007, 10:30 AM
My server side library is always returning a json string (already decoded by an interceptor at Ext.Ajax level), but the handleResponse in Ext.form.Action.Load and Submit don't verify the type before calling Ext.decode resulting in an error when it's already an object..

I changed the code in my local version to: (in Ext.form.Action.Load ans Submit


if (typeof response.responseText == 'string')
return Ext.decode(response.responseText);
return response.responseText;

ps.. or maybe do the check in Ext.decode.. if it's already an object just return it..

Thanks

Marco

tryanDLS
12 Jul 2007, 12:23 PM
I'm not sure that's a good solution. response.responseText is globally understood to be a text string, not an object.

mdissel
12 Jul 2007, 1:14 PM
But it doesn't affect the working of the method, it's only an extra validation if the responseText is indeed a string.

jack.slocum
12 Jul 2007, 4:42 PM
It's responseText, it will always be a string. If you are doing something with it that changes that, it would be your responsibility to make sure to change the appropriate spots that use it. Small changes like this start to add up, so we definitely would like to avoid them.

mdissel
13 Jul 2007, 12:09 AM
I'm only talking about one extra line in the handleResponse like this:


if (typeof response.responseText == 'string')
return Ext.decode(response.responseText);
return response.responseText;

i agree that changing the Ext.decode function is a bigger issue, but the above code doesn't change the inner working of handleresponse, it's just an extra check..

my alternative would be to override Ext.decode myself.. it this the correct code to override a function:

Ext.override(Ext.util.JSON,{
decode: function(json){
if (typeof json == 'string')
return Ext.util.JSON.superclass.decode.call(this, json);
return json;
}
});

Thanks

Marco

tryanDLS
13 Jul 2007, 9:09 AM
It's only a single line change, but it's not the correct approach as it undermines the implementation of a response object and it's known properties. This isn't an Ext thing - that's how the response object is defined.

As for your override, there's no superclass to call. You would use superclass to call the parent fn if you were extending a class.

Also, functionally it's the same, but use of braces won't cause it to kick out of a validator b/c of 2 return stmts in a row.



Ext.override(Ext.util.JSON,{
decode: function(json){
if (typeof json == 'string') {
return eval("(" + json + ')');
} else {
return json;
}
});