PDA

View Full Version : How to deal with null mapping in JsonReader ?



duskandawn
8 Sep 2010, 5:34 AM
hello All,
I have a JsonReader which looks like :


reader: new Ext.data.JsonReader({
totalProperty: 'count',
root : 'record',
fields: [
{name:'currency'},
{name: 'column1', type:'double',mapping: 'info.eqLevel1.value'},
{name: 'column2', type:'double',mapping: 'info.fixedLevel1.value',
//mapping: function(r){if(r.info.fixedLevel1 == null) return '';}
defaultValue : '0.00' }
});


So if the above mappings in any of my fields, results in a "Null" from (eg. info.fixedLevel1) my grid doesnt load.
I know i can use the mapping functions to return a blank for every null field, but since the response which i get back is from a 3rd party webservice call, i have to copy paste the same mapping function for the rest of the fields too,
which in my case will be changing very frequently and so have to update the front-end code evertime

I was looking for a centralized solution where any of the null fields which i know are fine should be replaced by a value of '0.00' in my case.
Where can i place this logic ? Reading the forum posts i got the solution for overriding the JsonReader below :



Ext.override(Ext.data.JsonReader, {
createAccessor : function(){
alert("hello");
var re = /[\[\.]/;
return function(expr) {
if(Ext.isEmpty(expr)){
return Ext.emptyFn;
}
if(Ext.isFunction(expr)){
return expr;
}
var i = String(expr).search(re);
if(i >= 0){
return new Function('obj', 'try {return obj' + (i > 0 ? '.' : '') + expr + "} catch(e) { return null;}");
}
return function(obj){
try {
return obj[expr];
} catch (e) {
return null;
}
};

};
}()
});

To place the try catch block around the object creation.
Can you please suggest if this is the right way of doing this, or there can be any better solution for my case, where i know the null values i am getting a valid values and they just need to be replace by a '0.00'.

Please advise,
Thanks,

Condor
8 Sep 2010, 5:42 AM
Try/catch blocks are really slow. I would recommend using:

{name: 'column1', type: 'double', mapping: function(v){
return v.info && v.info.eqLevel1 && v.info.eqLevel1.value ? v.info.eqLevel1.value : 0.0;
}, defaultValue: 0.0},
{name: 'column2', type: 'double', mapping: function(v){
return v.info && v.info.fixedLevel1 && v.info.fixedLevel1.value ? v.info.eqLevel1.value : 0.0;
}, defaultValue: 0.0}

duskandawn
8 Sep 2010, 6:00 AM
Hi Condor,
Thanks for the reply, i know i can use the (sample - commented part in my example)
mapping function as you suggested to make the checks on every field value, but i was looking for a centralized place where i can perform this check for every level2 field.
Any ideas where can i perform this check at ?
Reason, since the fields coming in, will be changing very frequently, i will have to write the mapping function for every single item.

Please advise,

Condor
8 Sep 2010, 6:09 AM
In that case a try/catch might be the easiest solution (but remember that you did this in case store loading becomes to slow).