ExtJS's Reader
Reader: Mainly used for the proxy data agent to read the data according to different rules to parse, the analysis of good data saved to Modle
Structure diagram
Ext.data.reader.Reader the root class of the reader
Ext.data.reader.Json Json-Formatted reader
Ext.data.reader.Array Array Reader for extended JSON
EXT.DATA.READER.XML XML-Formatted reader
Writer
Structure diagram
Ext.data.writer.Writer
Ext.data.writer.Json objects are interpreted as Json to be uploaded to the background
EXT.DATA.WRITER.XML objects are interpreted as Xml in the form of uploading to the background
1. JSON reader
[JavaScript]View PlainCopy
- (function () {
- Ext.onready (function () {
- var userData = {
- //total:200,
- COUNT:250,
- user:{
- UserID:' 1 ',
- Name:' uspcat.com ',
- orders:[
- {ID:' 001 ', Name:' Pen '},
- {ID:' 002 ', Name:' book '}
- ]
- }
- };
- //define Model
- Ext.regmodel ("user", {
- fields:[
- {name:' UserID ', type:' string '},
- {name: 'name ', type:' string '}
- ],
- Hasmany: {model: ' order '} //Define a property with multiple order
- });
- Ext.regmodel ("order", {
- fields:[
- {name: 'ID ', type:' string '},
- {name: 'name ', type:' string '}
- ],
- Belongsto: {type: ' Belongsto ', Model: ' user '} //definition belongs to
- });
- var mproxy = ext.create ("Ext.data.proxy.Memory", {
- Model:' user ',
- Data:userdata,
- reader:{
- Type:' json ',
- Root:' user ',
- Implicitincludes:True, //cascading read
- Totalproperty:' count '
- //record: ' Info '//The data returned by the server may be very complex, and a record can be used to delete the useful data information in the model
- }
- });
- Mproxy.read (new Ext.data.Operation (),function (result) {
- var datas = result.resultSet.records;
- alert (result.resultSet.total); //Print Count
- Ext.Array.each (Datas,function (model) {
- Alert (Model.get (' name '));
- });
- var user = result.resultset.records[0]; //Get the first data used
- var orders = User.orders (); //Get to the Orders collection in the user
- Orders.each (function (order) { //Traverse orders collection
- Alert (Order.get (' name '));
- });
- });
- });
- })();
2. Array Reader
[JavaScript]View PlainCopy
- Ext.onready (function () {
- Ext.regmodel ("person", {
- fields:[
- ' name ',' age '
- {name: ' Name '},
- {name: ' Age '}
- ],
- Proxy: {
- Type:' Ajax ',
- URL:' person.jsp ',
- reader:{
- Type:' array '
- }
- }
- });
- var person =ext.modelmanager.getmodel (' person ');
- Person.load (1,{
- Success:function (model) {
- Alert (Model.get (' name '));
- }
- });
- });
person.jsp file:
<%
Response.getwriter (). Write ("[[' Yunfengcheng ', 26]]");
%>
3. XML-Formatted Reader
[JavaScript]View PlainCopy
- (function () {
- Ext.onready (function () {
- Ext.regmodel ("user", {
- fields:[
- {name:' name '},
- {name:' ID '}
- ],
- proxy:{
- Type:' Ajax ',
- URL:' users.xml ',
- reader:{
- Type:' xml ',
- Record:' user '
- }
- }
- });
- var user = Ext.ModelManager.getModel (' user ');
- User.load (1,{
- Success:function (model) {
- alert (model);
- Alert (Model.get (' id '));
- }
- });
- });
- })();
Users.xml file:
[HTML]View PlainCopy
- <users>
- <user>
- <name>uspcat.com</name>
- <ID>00101</ID>
- </User>
- </users>
4. Writer's JSON and XML
[JavaScript]View PlainCopy
- Ext.onready (function () {
- Ext.regmodel ("person", {
- fields:[
- ' name ',' age '
- ],
- Proxy: {
- Type:' Ajax ',
- URL:' person.jsp ',
- writer:{
- Type:' json ' //Submit data using JSON
- //type: ' xml '//submit data Using XML
- }
- }
- });
- Ext.ModelMgr.create ({
- Name:' Uspcat.con ',
- Age:1
- },' person '). Save ();
- });
When writing with JSON, the browser views the commit information as if it were submitted in JSON:
When writing with XML, the browser views the commit information as if it were submitted in XML:
Original link: 8159774
ExtJS's Reader