Returns the following JSON from the service side:
{id:2,name: ' Child1 ', parent:{id:1,name: ' Parent '}}
The following jsonreader are defined to prepare a list of roles to be displayed, and the name of the parent role is mapped to ParentName:
var myReader = new Ext.data.JsonReader({
idProperty: 'id'
root: 'rows',
totalProperty: 'results',
fields: [
{name: 'Id'},
{name: 'Name'},
{name: 'ParentName', mapping: 'Parent.Name'}
]
});
When the server returns a JSON containing parent, like the JSON above, this reader works, but when the returned JSON does not contain parent or parent null, the list does not display correctly:
{id:2,name: ' Child1 ', parent:null}
You may have guessed mapping: ' Parent.name ' may have had a problem, yes, Parent is null, how can access the Name property, this time parentname should also be null. But Jsonreader did not take this into account, looked at the source code of the Jsonreader, and found the following method:
getJsonAccessor: function(){
var re = /[\[\.]/;
return function(expr) {
try {
return(re.test(expr)) ?
new Function("obj", "return obj." + expr) :
function(obj){
return obj[expr];
};
} catch(e){}
return Ext.emptyFn;
};
}(),