source: trunk/web/addons/job_monarch/lib/extjs/source/data/XmlReader.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 5.1 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10 * @class Ext.data.XmlReader
11 * @extends Ext.data.DataReader
12 * 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.<br><br>
14 * <p>
15 * <em>Note that in order for the browser to parse a returned XML document, the Content-Type
16 * header in the HTTP response must be set to "text/xml" or "application/xml".</em>
17 * <p>
18 * Example code:
19 * <pre><code>
20var Employee = Ext.data.Record.create([
21   {name: 'name', mapping: 'name'},     // "mapping" property not needed if it's the same as "name"
22   {name: 'occupation'}                 // This field will use "occupation" as the mapping.
23]);
24var myReader = new Ext.data.XmlReader({
25   totalRecords: "results", // The element which contains the total dataset size (optional)
26   record: "row",           // The repeated element which contains row information
27   id: "id"                 // The element within the row that provides an ID for the record (optional)
28}, Employee);
29</code></pre>
30 * <p>
31 * This would consume an XML file like this:
32 * <pre><code>
33&lt;?xml version="1.0" encoding="UTF-8"?>
34&lt;dataset>
35 &lt;results>2&lt;/results>
36 &lt;row>
37   &lt;id>1&lt;/id>
38   &lt;name>Bill&lt;/name>
39   &lt;occupation>Gardener&lt;/occupation>
40 &lt;/row>
41 &lt;row>
42   &lt;id>2&lt;/id>
43   &lt;name>Ben&lt;/name>
44   &lt;occupation>Horticulturalist&lt;/occupation>
45 &lt;/row>
46&lt;/dataset>
47</code></pre>
48 * @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
49 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
50 * paged from the remote server.
51 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
52 * @cfg {String} success The DomQuery path to the success attribute used by forms.
53 * @cfg {String} id The DomQuery path relative from the record element to the element that contains
54 * a record identifier value.
55 * @constructor
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}.
60 */
61Ext.data.XmlReader = function(meta, recordType){
62    meta = meta || {};
63    Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
64};
65Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
66    /**
67     * This method is only used by a DataProxy which has retrieved data from a remote server.
68         * @param {Object} response The XHR object which contains the parsed XML document.  The response is expected
69         * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
70     * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
71     * a cache of Ext.data.Records.
72     */
73    read : function(response){
74        var doc = response.responseXML;
75        if(!doc) {
76            throw {message: "XmlReader.read: XML Document not available"};
77        }
78        return this.readRecords(doc);
79    },
80
81    /**
82     * Create a data block containing Ext.data.Records from an XML document.
83         * @param {Object} doc A parsed XML document.
84     * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
85     * a cache of Ext.data.Records.
86     */
87    readRecords : function(doc){
88        /**
89         * After any data loads/reads, the raw XML Document is available for further custom processing.
90         * @type XMLDocument
91         */
92        this.xmlData = doc;
93        var root = doc.documentElement || doc;
94        var q = Ext.DomQuery;
95        var recordType = this.recordType, fields = recordType.prototype.fields;
96        var sid = this.meta.id;
97        var totalRecords = 0, success = true;
98        if(this.meta.totalRecords){
99            totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
100        }
101
102        if(this.meta.success){
103            var sv = q.selectValue(this.meta.success, root, true);
104            success = sv !== false && sv !== 'false';
105        }
106        var records = [];
107        var ns = q.select(this.meta.record, root);
108        for(var i = 0, len = ns.length; i < len; i++) {
109                var n = ns[i];
110                var values = {};
111                var id = sid ? q.selectValue(sid, n) : undefined;
112                for(var j = 0, jlen = fields.length; j < jlen; j++){
113                    var f = fields.items[j];
114                var v = q.selectValue(f.mapping || f.name, n, f.defaultValue);
115                    v = f.convert(v, n);
116                    values[f.name] = v;
117                }
118                var record = new recordType(values, id);
119                record.node = n;
120                records[records.length] = record;
121            }
122
123            return {
124                success : success,
125                records : records,
126                totalRecords : totalRecords || records.length
127            };
128    }
129});
Note: See TracBrowser for help on using the repository browser.