ExtJS Practice: Support for "complex" JSON jsonreader

Source: Internet
Author: User
Tags json

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;
     };
   }(),

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.