Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS - null safe retrieval of complex objects using JsonReader

Tags:

extjs

I am using a JsonReader to map Json data to variables to be used in a grid/form. The back end is in Java and there are complex objects which I Jsonify and pass to the ExtJS front end. This is a part of my JsonReader which tries to retrieve a nested object -

{name:'status', type: 'string', mapping: 'status.name'}

This works fine when status has a value (not null in the server), but the grid load fails when status is null. Currently the work around I have is to send an empty object from the server in case of null, but I assume there should be a way to handle this in ExtJS. Please suggest a better solution on the ExtJS side.

like image 393
Joji Avatar asked May 25 '26 07:05

Joji


1 Answers

I can think of two possibilities - one documented and one undocumented:

  1. use the convert()-mechanism of Ext.data.Field:

    {
        name:'status', 
        mapping: 'status',
        convert: function(status, data) {
            if (!Ext.isEmpty(status) && status.name) {
                return status.name;
            } else {
                return null;
            }
        }
    }
    
  2. The mapping property can also take an extractor function (this is undocumented so perhaps it may be a little bit risky to rely on this):

    {
        name:'status', 
        mapping: function(data) {
            if (data.status && data.status.name) {
                return data.status.name;
            } else {
                return null;
            }
        }
    }
    
like image 138
Stefan Gehrig Avatar answered May 27 '26 14:05

Stefan Gehrig