113 lines
No EOL
5.3 KiB
HTML
113 lines
No EOL
5.3 KiB
HTML
<html><head><title>XmlReader.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>XmlReader.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Ext.data.XmlReader
|
|
* @extends Ext.data.DataReader
|
|
* Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
|
|
* based on mappings <b>in</b> a provided Ext.data.Record constructor.
|
|
* <p>
|
|
* The code below lists all configuration options.
|
|
* <pre><code>
|
|
<b>var</b> myReader = <b>new</b> Ext.data.XmlReader({
|
|
record: "row", <i>// The repeated element which contains record information</i>
|
|
totalRecords: "results", <i>// The element which contains the number of returned records (optional)</i>
|
|
id: "id" <i>// The element within the record that provides an ID <b>for</b> the record (optional)</i>
|
|
}, myRecordDefinition);
|
|
</code></pre>
|
|
* <p>
|
|
* This would consume an XML file like <b>this</b>:
|
|
* <pre><code>
|
|
&lt;?xml?>
|
|
&lt;dataset>
|
|
&lt;results>2&lt;/results>
|
|
&lt;row>
|
|
&lt;id>1&lt;/id>
|
|
&lt;name>Bill&lt;/name>
|
|
&lt;/row>
|
|
&lt;row>
|
|
&lt;id>2&lt;/id>
|
|
&lt;name>Ben&lt;/name>
|
|
&lt;/row>
|
|
&lt;/dataset>
|
|
</code></pre>
|
|
* @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
|
|
* <b>in</b> the dataset. This is only needed <b>if</b> the whole dataset is not passed <b>in</b> one go, but is being
|
|
* paged from the remote server.
|
|
* @cfg {String} record The DomQuery path to the repeated element which contains record information.
|
|
* @cfg {String} success The DomQuery path to the success attribute used by forms.
|
|
* @cfg {String} id The DomQuery path relative from the record element to the element that contains
|
|
* a record identifier value.
|
|
* @constructor
|
|
* Create a <b>new</b> XmlReader
|
|
* @param {Object} meta Metadata configuration options
|
|
* @param {Mixed} recordType The definition of the data record type to produce. This can be either a valid
|
|
* Record subclass created <b>with</b> {@link Ext.data.Record#create}, or an array of objects <b>with</b> which to call
|
|
* Ext.data.Record.create. See the {@link Ext.data.Record} class <b>for</b> more details.
|
|
*/</i>
|
|
Ext.data.XmlReader = <b>function</b>(meta, recordType){
|
|
Ext.data.XmlReader.superclass.constructor.call(<b>this</b>, meta, recordType);
|
|
};
|
|
Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
|
|
<i>/**
|
|
* This method is only used by a DataProxy which has retrieved data from a remote server.
|
|
* @param {Object} response The XHR object which contains the parsed XML document. The response is expected
|
|
* to contain a method called 'responseXML' that returns an XML document object.
|
|
* @<b>return</b> {Object} records A data block which is used by an {@link Ext.data.Store} as
|
|
* a cache of Ext.data.Records.
|
|
*/</i>
|
|
read : <b>function</b>(response){
|
|
<b>var</b> doc = response.responseXML;
|
|
<b>if</b>(!doc) {
|
|
throw {message: "XmlReader.read: XML Document not available"};
|
|
}
|
|
<b>return</b> this.readRecords(doc);
|
|
},
|
|
|
|
<i>/**
|
|
* Create a data block containing Ext.data.Records from an XML document.
|
|
* @param {Object} doc A parsed XML document.
|
|
* @<b>return</b> {Object} records A data block which is used by an {@link Ext.data.Store} as
|
|
* a cache of Ext.data.Records.
|
|
*/</i>
|
|
readRecords : <b>function</b>(doc){
|
|
<i>/**
|
|
* After any data loads/reads, the raw XML Document is available <b>for</b> further custom processing.
|
|
* @type XMLDocument
|
|
*/</i>
|
|
<b>this</b>.xmlData = doc;
|
|
<b>var</b> root = doc.documentElement || doc;
|
|
<b>var</b> q = Ext.DomQuery;
|
|
<b>var</b> recordType = <b>this</b>.recordType, fields = recordType.prototype.fields;
|
|
<b>var</b> sid = <b>this</b>.meta.id;
|
|
<b>var</b> totalRecords = 0, success = true;
|
|
<b>if</b>(this.meta.totalRecords){
|
|
totalRecords = q.selectNumber(<b>this</b>.meta.totalRecords, root, 0);
|
|
}
|
|
|
|
<b>if</b>(this.meta.success){
|
|
<b>var</b> sv = q.selectValue(<b>this</b>.meta.success, root, true);
|
|
success = sv !== false && sv !== 'false';
|
|
}
|
|
<b>var</b> records = [];
|
|
<b>var</b> ns = q.select(<b>this</b>.meta.record, root);
|
|
<b>for</b>(var i = 0, len = ns.length; i < len; i++) {
|
|
<b>var</b> n = ns[i];
|
|
<b>var</b> values = {};
|
|
<b>var</b> id = sid ? q.selectValue(sid, n) : undefined;
|
|
<b>for</b>(var j = 0, jlen = fields.length; j < jlen; j++){
|
|
<b>var</b> f = fields.items[j];
|
|
<b>var</b> v = q.selectValue(f.mapping || f.name, n, f.defaultValue);
|
|
v = f.convert(v);
|
|
values[f.name] = v;
|
|
}
|
|
<b>var</b> record = <b>new</b> recordType(values, id);
|
|
record.node = n;
|
|
records[records.length] = record;
|
|
}
|
|
|
|
<b>return</b> {
|
|
success : success,
|
|
records : records,
|
|
totalRecords : totalRecords || records.length
|
|
};
|
|
}
|
|
});</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |