Jsonreader
In JavaScript, JSON is a very important data format. The form of key: value is easier to understand than the complicated tag structure of XML, and the amount of code is smaller, many people tend to use it as the data exchange format of Ext. The code below shows the JSON data prepared for JSON-reader.
var data = {id:0,totalProperty:2,successProperty:true,root:[{id:'id1',name:'name1',descn:'descn1'},{id:'id2',name:'name2',descn:'descn2'}]}; |
Compared with arrays, JSON supports pagination. The totalproperty parameter can be used to represent the total amount of data. The successproperty parameter is optional. You can use it to determine whether the current request is successfully executed and whether data is loaded. If you do not want jsonreader to process the response data, you can set successproperty to false.
Now let's take a look at jsonreader to see how it corresponds to the above JSON data, as shown in the following code.
var reader = new Ext.data.JsonReader({successProperty: "successproperty",totalProperty: "totalProperty",root: "root",id: "id"}, [{name:'id',mapping:'id'},{name:'name',mapping:'name'},{name:'descn',mapping:'descn'}]); |
In the preceding example, the ing method is not concise, because the content of name and mapping is the same. In fact, mapping can be omitted here. By default, the name parameter is used to obtain the corresponding data from JSON. If you do not want to use the same name as in JSON, you can use mapping to modify the name. However, mapping has other purposes here, as shown in 10-3 of the code list.
Code List 10-3 configure mapping for jsonreader
var data = {id:0,totalProperty:2,successProperty:true,root:[{id:'id1',name:'name1',descn:'descn1',person:{id:1,name:'man',sex:'male'}},{id:'id2',name:'name2',descn:'descn2',person:{id:2,name:'woman',sex:'female'}}]};var reader = new Ext.data.JsonReader({successProperty: "successproperty",totalProperty: "totalProperty",root: "root",id: "id"}, ['id','name','descn',{name:'person_name',mapping:'person.name'},{name:'person_sex',mapping:'person.sex'}]); |
In the code above, we use JSON to support more complex nested structures. The person object itself has attributes such as ID, name, and sex. In jsonreader, you can use mapping to map these nested internal attributes to the corresponding record, while other fields remain unchanged.