读写器reader和writer详解


一、读取器Ext.data.reader.Reader 根类

       1、JSON格式的读取器

              (1)定义数据集(例子使用内存代理)

                     varuserData={

                     //total:200,

                     count:250,

                     user:[autorth:"zhang",info{

                            userID:1,

                            name:"marico",

                            orders:[

                                   {id:"001",name:"pen"},

                                   {id:"002",name:"book"}

                            ]

                     }]

                     };

              (2)定义model数据模型 配置一对多关联

              配置一对多关系 ,hasmany字段指定一个对象

                     model指定的是多方的数据模型

                     name指定的属性名,如果配置name则使用这个名字来获取级联的记录,如果不配置系统自动生成为orders

              Ext.regModel("user",{

                     fields:[

                            {name:"userID",type:"string"},

                            {name:"name",type:"string"}

                     ],

                     hasMany:{

                            model:"order"

                            //name:"order"

                     }

              });

              配置多对一关系,belongTo指定一个对象

              参数type值得是什么关联,belongTo代表多对一

              model指定一方的数据模型

              Ext.regModel("order",{

                     fields:[

                            {name:"id",type:"string"},

                            {name:"name",type:"string"}

                     ],

                     belongTo:{type:"belongTo",model:"user"}

              });

              (3)创建内存代理并指定reader

               reader指定的是对象

                     type指定的是什么数据 常用json和xml

                     root指定的是得到数据的根,也就想得到的数据的对象名,上方userData中的user

                     implicitIncludes默认为true 设置是否级联操作

                     totalProperty指定的是读取total数据的属性名  如果没有系统则默认为1

                     record对服务器返回的数据可能很复杂,用record可以筛选出有用的的数据信息,装载到Model中

              varmproxy=Ext.create("Ext.data.proxy.Memory",{

                     model:"user",

                     data:userData,

                     reader:{

                            type:"json",

                            root:"user",

                            implicitIncludes:true,

                            totalProperty:"count",//上方已经设置为count

                            record:"info" 

                     }

              });

              (4)调用代理中的read方法遍历并输出元素

                     mproxy.read(newExt.data.Operation(),function(result){

                           

                            vardatas=result.resultSet.records;//存放数据的对象数组

                            //alert(result);

                            alert(result.resultSet.total);

                            //利用ExtArray中的each方法遍历数组

                            Ext.Array.each(datas,function(model){

                                   alert(model.get("name"));

                            });

                            //得到一个user对象

                            varuser=result.resultSet.records[0];

                            varorders=user.orders(); //调用orders方法就可以得到里面orders数据,返回的是store数据集

                            //调用Ext.data.Store中each方法遍历

                            orders.each(function(order){

                                   alert(order.get("name"));

                            });

                     });

             

             

                    

       2、XML格式的读取器

              (1)创建xml文档

                     <users>

                            <user>

                                   <name>marico</name>

                                   <id>00101</id>

                            </user>

                     </users>

              (2)创建数据模型并指定reader中的type属性为xml

             

                     Ext.regModel("user",{

                     fields:[

                            {name:"name"},

                            {name:"id"}

                     ],

                     proxy:{

                            type:"ajax",

                            url:"users.xml",

                            reader:{

                                   type:"xml",

                                   record:"user"

                            }

                     }

              });

              (3)创建对象并调用load方法遍历

              varuser=Ext.ModelManager.getModel("user");

              user.load(1,{

                     success:function(model){

                            alert(model.get("name"));

                     }

              });

       2、Array格式的读取器

              定义model 可以将其属性制定成简单的数组

              返回信息的格式:[['marico',21],['zhang',20]]

              Ext.regModel("user",{

                     fields:[   

                     'name','age'

                     ],

                     proxy:{

                            type:"ajax",

                            url:"user.jsp",

                            reader:{

                                   type:"array",

                            }

                     }

              });

              varuser=Ext.ModelManager.getModel("user");

              user.load(1,{

                     success:function(model){

                            alert(model.get("name"));

                     }

              });

二、写Ext.data.writer.writer

       1定义model并指定writer已什么形式响应

              type指定响应格式  json 和xml  则就可以在后台解析出来数据

             

              Ext.onReady(function(){

              Ext.regModel("person",{

              fields:[

                     'name','age'

                     //{name:"name"},

                     //{name:"age"}

              ],

              proxy:{

                     type:"ajax",

                     url:"person.jsp",

                     reader:{

                            type:"array"

                     },

                     writer:{

                            type:"json"

                     }

              }

       });

       2、实例化对象并调用save方法

       Ext.ModelMgr.create({

              name:"marico",

              age:21

       },'person').save();  

       

你可能感兴趣的:(xml,json,Ajax,function,String,user)