Ext.dataテーマ六:よく使うReader


Ext.dataテーマ六:よく使うReader
  Reader ArrayReader

 proxy            ,       Record        Ext.data. Store  。
ArrayReader                ,       Record。
                  ,         mapping  record          。
ArrayReader      ,         。
                 :

var data = [
    ['id1','name1','descn1'],
    ['id2','name2','descn2']
];


   ArrayReader        :

var reader = new Ext.data.ArrayReader({
    id:1
},[
    {name:'name',mapping:1},
    {name:'descn',mapping:2},
    {name:'id',mapping:0},
]);


  Reader JsonReader

 JavaScript ,JSON            ,key:value    XML              ,      ,           EXT       。
 Json- Reader   JSON          :

var data = {
    id:0,
    totalProperty:2,
    successProperty:true,
    root:[
        {id:'id1',name:'name1',descn:'descn1'},
        {id:'id2',name:'name2',descn:'descn2'}
    ]
};


     ,JSON           ,      totalProperty         。
successProperty      ,                ,            。
    JsonReader       ,   successProperty   false。
       JsonReader,          JSON     ,        :

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'}
]);


            ,  name mapping         ,     mapping    ,    name   JSON        。
     JSON      ,     mapping  。  ,mapping         ,    :


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'}
]);


       ,    JSON          ,   person       id、 name sex   。
 JsonReader    mapping              ,     record,        。

  Reader XmlReader

XML            ,XmlReader   XML         :

<?xml version="1.0" encoding="utf-8"?>
<dataset>
    <id>1</id>
    <totalRecords>2</totalRecords>
    <success>true</success>
    <record>
        <id>1</id>
        <name>name1</name>
        <descn>descn1</descn>
    </record>
    <record>
        <id>2</id>
        <name>name2</name>
        <descn>descn2</descn>
    </record>
</dataset>


      dataset  XML   。          XmlReader    ,          XML  ,        :


var reader = new Ext.data.XmlReader({
   totalRecords: 'totalRecords',
   success: 'success'
   record: 'record',
   id: "id"
}, ['id','name','descn']);


XmlReader           JsonReader    ,           totalRecords record    ,
  totalRecords     ’totalRecords’           ,record   XML   record                  。
      success id    JsonReader        ,                 id。
  XML     reader          ,       ,
 [{name:’id’},{name:’name’},{name:’descn’}]
     [‘id’,’name’,’descn’]。
  XmlReader   JavaScript          XML     ,                。
  localXHR.js   XML   ,           ,     :

var data = "<?xml version='1.0' encoding='utf-8'?>" +
    "<dataset>" +
        "<id>1</id>" +
        "<totalRecords>2</totalRecords>" +
        "<success>true</success>" +
        "<record>" +
            "<id>1</id>" +
            "<name>name1</name>" +
            "<descn>descn1</descn>" +
        "</record>" +
        "<record>" +
            "<id>2</id>" +
            "<name>name2</name>" +
            "<descn>descn2</descn>" +
        "</record>" +
    "</dataset>";
 
var xdoc;
 
if(typeof(DOMParser) == 'undefined'){
    xdoc = new ActiveXObject("Microsoft.XMLDOM");
    xdoc.async="false";
    xdoc.loadXML(data);
}else{
    var domParser = new DOMParser();
    xdoc = domParser.parseFromString(data, 'application/xml');
    domParser = null;
}
 
var proxy = new Ext.data.MemoryProxy(xdoc);
 
var reader = new Ext.data.XmlReader({
    totalRecords: 'totalRecords',
    success: 'success',
    record: 'record',
    id: "id"
}, ['id','name','descn']);
 
var ds = new Ext.data.Store({
    proxy: proxy,
    reader: reader
});