View Full Version : Ext.data.JsonReader suggestion
My suggestion for JsonReader.read is add a "try" block on eval()
var o = eval("("+json+")");
from JsonReader.js:
Ext.extend(Ext.data.JsonReader, Ext.data.DataReader, {
read : function(response){
var json = response.responseText;
try {
var o = eval("("+json+")");
} catch(e) {
var o = {};
}
It's avoid JavaScript errors when the server-side response is a non-JSON response (like a PHP/MySQL error)
aconran
22 Aug 2007, 2:22 PM
If you want JsonReader to behave like this I would suggest overriding JsonReader's implementation using override.
I can see how people would argue for this to work both ways (Failing with an error or silently failing). If JsonReader tries to evaluate a non-JSON response you at least see a JS error as opposed to silently failing as you have suggested.
These JS errors could aid in the development process. It's possible that a request would silently fail and developers may have difficulty tracking it down. On the other hand if your site is in production you may want to have your suggested behaviour as you never want to show your clients/customers any errors that may occur.
I typically create a file called overrides.js and place it in my "ext/" directory with all of my overrides.
The syntax:
Ext.data.JsonReader.override({
read : function(response) {
// your implementation here
}
});
aconran, thanks for your help
Here is my Ext.data.JsonReader.read suggestion:
Ext.data.JsonReader.override({
read: function(response) {
var json = response.responseText, o = {}, error = false, root = this.meta.root;
try {
var o = eval('('+json+')');
} catch(e) {
error = true;
}
if( !o[root] ) o[root] = {};
if( o.metaData ) {
delete this.ef;
this.meta = o.metaData;
this.recordType = Ext.data.Record.create(o.metaData.fields);
this.onMetaChange(this.meta, this.recordType, o);
}
return ( !error ? this.readRecords(o) : o );
}
});The JavaScript error occurs in two conditions:
1) When the server-side response ISN'T in JSON format (eg: a PHP/MYSQL error)
2) When the response "IS" in JSON format, BUT doesn't have the data node
Eg: the data node is called "results" and the server returns {}
So readRecord tries get the "length" from a undefined value and JavaScript error appears
I report it as a bug:
http://extjs.com/forum/showthread.php?p=56755
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.