3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.data.XmlReader"></div>/**
16 * @class Ext.data.XmlReader
17 * @extends Ext.data.DataReader
18 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
19 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
20 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
21 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
22 * <p>Example code:</p>
24 var Employee = Ext.data.Record.create([
25 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
26 {name: 'occupation'} // This field will use "occupation" as the mapping.
28 var myReader = new Ext.data.XmlReader({
29 totalProperty: "results", // The element which contains the total dataset size (optional)
30 record: "row", // The repeated element which contains row information
31 idProperty: "id" // The element within the row that provides an ID for the record (optional)
32 messageProperty: "msg" // The element within the response that provides a user-feedback message (optional)
36 * This would consume an XML file like this:
38 <?xml version="1.0" encoding="UTF-8"?>
40 <results>2</results>
43 <name>Bill</name>
44 <occupation>Gardener</occupation>
48 <name>Ben</name>
49 <occupation>Horticulturalist</occupation>
53 * @cfg {String} totalProperty The DomQuery path from which to retrieve the total number of records
54 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
55 * paged from the remote server.
56 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
57 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
58 * @cfg {String} successProperty The DomQuery path to the success attribute used by forms.
59 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
60 * a record identifier value.
62 * Create a new XmlReader.
63 * @param {Object} meta Metadata configuration options
64 * @param {Object} recordType Either an Array of field definition objects as passed to
65 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
67 Ext.data.XmlReader = function(meta, recordType){
70 // backwards compat, convert idPath or id / success
72 idProperty: meta.idProperty || meta.idPath || meta.id,
73 successProperty: meta.successProperty || meta.success
76 Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
78 Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
80 * This method is only used by a DataProxy which has retrieved data from a remote server.
81 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
82 * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
83 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
84 * a cache of Ext.data.Records.
86 read : function(response){
87 var doc = response.responseXML;
89 throw {message: "XmlReader.read: XML Document not available"};
91 return this.readRecords(doc);
95 * Create a data block containing Ext.data.Records from an XML document.
96 * @param {Object} doc A parsed XML document.
97 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
98 * a cache of Ext.data.Records.
100 readRecords : function(doc){
102 * After any data loads/reads, the raw XML Document is available for further custom processing.
107 var root = doc.documentElement || doc,
112 if(this.meta.totalProperty){
113 totalRecords = this.getTotal(root, 0);
115 if(this.meta.successProperty){
116 success = this.getSuccess(root);
119 var records = this.extractData(q.select(this.meta.record, root), true); // <-- true to return Ext.data.Record[]
121 // TODO return Ext.data.Response instance. @see #readResponse
125 totalRecords : totalRecords || records.length
130 * Decode an XML response from server.
131 * @param {String} action [{@link Ext.data.Api#actions} create|read|update|destroy]
132 * @param {Object} response HTTP Response object from browser.
133 * @return {Ext.data.Response} An instance of {@link Ext.data.Response}
135 readResponse : function(action, response) {
136 var q = Ext.DomQuery,
137 doc = response.responseXML;
139 // create general Response instance.
140 var res = new Ext.data.Response({
142 success : this.getSuccess(doc),
143 message: this.getMessage(doc),
144 data: this.extractData(q.select(this.meta.record, doc) || q.select(this.meta.root, doc), false),
148 if (Ext.isEmpty(res.success)) {
149 throw new Ext.data.DataReader.Error('successProperty-response', this.meta.successProperty);
152 // Create actions from a response having status 200 must return pk
153 if (action === Ext.data.Api.actions.create) {
154 var def = Ext.isDefined(res.data);
155 if (def && Ext.isEmpty(res.data)) {
156 throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
159 throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
165 getSuccess : function() {
170 * build response-data extractor functions.
174 buildExtractors : function() {
179 Record = this.recordType,
180 f = Record.prototype.fields,
184 if(s.totalProperty) {
185 this.getTotal = this.createAccessor(s.totalProperty);
187 if(s.successProperty) {
188 this.getSuccess = this.createAccessor(s.successProperty);
190 if (s.messageProperty) {
191 this.getMessage = this.createAccessor(s.messageProperty);
193 this.getRoot = function(res) {
194 return (!Ext.isEmpty(res[this.meta.record])) ? res[this.meta.record] : res[this.meta.root];
196 if (s.idPath || s.idProperty) {
197 var g = this.createAccessor(s.idPath || s.idProperty);
198 this.getId = function(rec) {
199 var id = g(rec) || rec.id;
200 return (id === undefined || id === '') ? null : id;
203 this.getId = function(){return null;};
206 for(var i = 0; i < fl; i++){
208 var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
209 ef.push(this.createAccessor(map));
215 * Creates a function to return some particular key of data from a response.
216 * @param {String} key
221 createAccessor : function(){
222 var q = Ext.DomQuery;
223 return function(key) {
225 case this.meta.totalProperty:
226 return function(root, def){
227 return q.selectNumber(key, root, def);
230 case this.meta.successProperty:
231 return function(root, def) {
232 var sv = q.selectValue(key, root, true);
233 var success = sv !== false && sv !== 'false';
238 return function(root, def) {
239 return q.selectValue(key, root, def);
247 * extracts values and type-casts a row of data from server, extracted by #extractData
249 * @param {Ext.data.Field[]} items
250 * @param {Number} len
254 extractValues : function(data, items, len) {
256 for(var j = 0; j < len; j++){
258 var v = this.ef[j](data);
259 values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);