3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.data.XmlReader"></div>/**
10 * @class Ext.data.XmlReader
11 * @extends Ext.data.DataReader
12 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
13 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
14 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
15 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
16 * <p>Example code:</p>
18 var Employee = Ext.data.Record.create([
19 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
20 {name: 'occupation'} // This field will use "occupation" as the mapping.
22 var myReader = new Ext.data.XmlReader({
23 totalProperty: "results", // The element which contains the total dataset size (optional)
24 record: "row", // The repeated element which contains row information
25 idProperty: "id" // The element within the row that provides an ID for the record (optional)
26 messageProperty: "msg" // The element within the response that provides a user-feedback message (optional)
30 * This would consume an XML file like this:
32 <?xml version="1.0" encoding="UTF-8"?>
34 <results>2</results>
37 <name>Bill</name>
38 <occupation>Gardener</occupation>
42 <name>Ben</name>
43 <occupation>Horticulturalist</occupation>
47 * @cfg {String} totalProperty The DomQuery path from which to retrieve the total number of records
48 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
49 * paged from the remote server.
50 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
51 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
52 * @cfg {String} successProperty The DomQuery path to the success attribute used by forms.
53 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
54 * a record identifier value.
56 * Create a new XmlReader.
57 * @param {Object} meta Metadata configuration options
58 * @param {Object} recordType Either an Array of field definition objects as passed to
59 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
61 Ext.data.XmlReader = function(meta, recordType){
64 // backwards compat, convert idPath or id / success
66 idProperty: meta.idProperty || meta.idPath || meta.id,
67 successProperty: meta.successProperty || meta.success
70 Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
72 Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
74 * This method is only used by a DataProxy which has retrieved data from a remote server.
75 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
76 * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
77 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
78 * a cache of Ext.data.Records.
80 read : function(response){
81 var doc = response.responseXML;
83 throw {message: "XmlReader.read: XML Document not available"};
85 return this.readRecords(doc);
89 * Create a data block containing Ext.data.Records from an XML document.
90 * @param {Object} doc A parsed XML document.
91 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
92 * a cache of Ext.data.Records.
94 readRecords : function(doc){
96 * After any data loads/reads, the raw XML Document is available for further custom processing.
101 var root = doc.documentElement || doc,
106 if(this.meta.totalProperty){
107 totalRecords = this.getTotal(root, 0);
109 if(this.meta.successProperty){
110 success = this.getSuccess(root);
113 var records = this.extractData(q.select(this.meta.record, root), true); // <-- true to return Ext.data.Record[]
115 // TODO return Ext.data.Response instance. @see #readResponse
119 totalRecords : totalRecords || records.length
124 * Decode a json response from server.
125 * @param {String} action [{@link Ext.data.Api#actions} create|read|update|destroy]
126 * @param {Object} response HTTP Response object from browser.
127 * @return {Ext.data.Response} response Returns an instance of {@link Ext.data.Response}
129 readResponse : function(action, response) {
130 var q = Ext.DomQuery,
131 doc = response.responseXML;
133 // create general Response instance.
134 var res = new Ext.data.Response({
136 success : this.getSuccess(doc),
137 message: this.getMessage(doc),
138 data: this.extractData(q.select(this.meta.record, doc) || q.select(this.meta.root, doc), false),
142 if (Ext.isEmpty(res.success)) {
143 throw new Ext.data.DataReader.Error('successProperty-response', this.meta.successProperty);
146 // Create actions from a response having status 200 must return pk
147 if (action === Ext.data.Api.actions.create) {
148 var def = Ext.isDefined(res.data);
149 if (def && Ext.isEmpty(res.data)) {
150 throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
153 throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
159 getSuccess : function() {
164 * build response-data extractor functions.
168 buildExtractors : function() {
173 Record = this.recordType,
174 f = Record.prototype.fields,
178 if(s.totalProperty) {
179 this.getTotal = this.createAccessor(s.totalProperty);
181 if(s.successProperty) {
182 this.getSuccess = this.createAccessor(s.successProperty);
184 if (s.messageProperty) {
185 this.getMessage = this.createAccessor(s.messageProperty);
187 this.getRoot = function(res) {
188 return (!Ext.isEmpty(res[this.meta.record])) ? res[this.meta.record] : res[this.meta.root];
190 if (s.idPath || s.idProperty) {
191 var g = this.createAccessor(s.idPath || s.idProperty);
192 this.getId = function(rec) {
193 var id = g(rec) || rec.id;
194 return (id === undefined || id === '') ? null : id;
197 this.getId = function(){return null;};
200 for(var i = 0; i < fl; i++){
202 var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
203 ef.push(this.createAccessor(map));
209 * Creates a function to return some particular key of data from a response.
210 * @param {String} key
215 createAccessor : function(){
216 var q = Ext.DomQuery;
217 return function(key) {
219 case this.meta.totalProperty:
220 return function(root, def){
221 return q.selectNumber(key, root, def);
224 case this.meta.successProperty:
225 return function(root, def) {
226 var sv = q.selectValue(key, root, true);
227 var success = sv !== false && sv !== 'false';
232 return function(root, def) {
233 return q.selectValue(key, root, def);
241 * extracts values and type-casts a row of data from server, extracted by #extractData
243 * @param {Ext.data.Field[]} items
244 * @param {Number} len
248 extractValues : function(data, items, len) {
250 for(var j = 0; j < len; j++){
252 var v = this.ef[j](data);
253 values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);