Enabling use of “defaultValue” with JSON data source for Ext JS

I have a JSON response that has data in a nested object within a data entry. Mapping and reading the nested data is easy and straightforward. The interesting part is that the nested object is optional and therefore may not exist in the response sometimes. Unfortunately, JsonReader can’t handle this and would throw a silent exception that caused the whole UI initialization to halt. The API doc seem to suggest that the proper behavior is for the library to use pre-configured defaultValue when data doesn’t exist. Here is the original code from the readRecords function from JsonReader:

for(var j = 0; j < fl; j++){
  f = fi[j];
  var v = this.ef[j](n);
  values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, n);

Exception would occur at the point of calling this.ef[j](n) to access the data at field index j from the parameter n, which contains JSON fragment. This prevented the defaultValue ever being used in the next line.

What I did to work around this issue is to add a try-catch block around the line and set v to undefined in the catch clause. This allows the defaultValue to be used when data is missing.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s