3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.XmlWriter
9 * @extends Ext.data.DataWriter
10 * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action via XML.
12 Ext.data.XmlWriter = Ext.extend(Ext.data.DataWriter, {
14 * Final action of a write event. Apply the written data-object to params.
15 * @param {String} action [Ext.data.Api.create|read|update|destroy]
16 * @param {Record[]} rs
17 * @param {Object} http params
18 * @param {Object} data object populated according to DataReader meta-data "root" and "idProperty"
20 render : function(action, rs, params, data) {
25 * @param {Ext.data.Record} rec
27 createRecord : function(rec) {
32 * @param {Ext.data.Record} rec
34 updateRecord : function(rec) {
40 * @param {Ext.data.Record} rec
42 destroyRecord : function(rec) {
46 * @class Ext.data.XmlReader
47 * @extends Ext.data.DataReader
48 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
49 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
50 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
51 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
52 * <p>Example code:</p>
54 var Employee = Ext.data.Record.create([
55 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
56 {name: 'occupation'} // This field will use "occupation" as the mapping.
58 var myReader = new Ext.data.XmlReader({
59 totalRecords: "results", // The element which contains the total dataset size (optional)
60 record: "row", // The repeated element which contains row information
61 id: "id" // The element within the row that provides an ID for the record (optional)
65 * This would consume an XML file like this:
67 <?xml version="1.0" encoding="UTF-8"?>
69 <results>2</results>
72 <name>Bill</name>
73 <occupation>Gardener</occupation>
77 <name>Ben</name>
78 <occupation>Horticulturalist</occupation>
82 * @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
83 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
84 * paged from the remote server.
85 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
86 * @cfg {String} success The DomQuery path to the success attribute used by forms.
87 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
88 * a record identifier value.
90 * Create a new XmlReader.
91 * @param {Object} meta Metadata configuration options
92 * @param {Object} recordType Either an Array of field definition objects as passed to
93 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
95 Ext.data.XmlReader = function(meta, recordType){
97 Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
99 Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
101 * This method is only used by a DataProxy which has retrieved data from a remote server.
102 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
103 * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
104 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
105 * a cache of Ext.data.Records.
107 read : function(response){
108 var doc = response.responseXML;
110 throw {message: "XmlReader.read: XML Document not available"};
112 return this.readRecords(doc);
116 * Create a data block containing Ext.data.Records from an XML document.
117 * @param {Object} doc A parsed XML document.
118 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
119 * a cache of Ext.data.Records.
121 readRecords : function(doc){
123 * After any data loads/reads, the raw XML Document is available for further custom processing.
127 var root = doc.documentElement || doc;
128 var q = Ext.DomQuery;
129 var recordType = this.recordType, fields = recordType.prototype.fields;
130 var sid = this.meta.idPath || this.meta.id;
131 var totalRecords = 0, success = true;
132 if(this.meta.totalRecords){
133 totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
136 if(this.meta.success){
137 var sv = q.selectValue(this.meta.success, root, true);
138 success = sv !== false && sv !== 'false';
141 var ns = q.select(this.meta.record, root);
142 for(var i = 0, len = ns.length; i < len; i++) {
145 var id = sid ? q.selectValue(sid, n) : undefined;
146 for(var j = 0, jlen = fields.length; j < jlen; j++){
147 var f = fields.items[j];
148 var v = q.selectValue(Ext.value(f.mapping, f.name, true), n, f.defaultValue);
152 var record = new recordType(values, id);
154 records[records.length] = record;
160 totalRecords : totalRecords || records.length
164 // TODO: implement readResponse for XmlReader
165 readResponse : Ext.emptyFn
167 * @class Ext.data.XmlStore
\r
168 * @extends Ext.data.Store
\r
169 * <p>Small helper class to make creating {@link Ext.data.Store}s from XML data easier.
\r
170 * A XmlStore will be automatically configured with a {@link Ext.data.XmlReader}.</p>
\r
171 * <p>A store configuration would be something like:<pre><code>
\r
172 var store = new Ext.data.XmlStore({
\r
175 storeId: 'myStore',
\r
176 url: 'sheldon.xml', // automatically configures a HttpProxy
\r
178 record: 'Item', // records will have an "Item" tag
\r
180 totalRecords: '@TotalResults'
\r
182 // set up the fields mapping into the xml doc
\r
183 // The first needs mapping, the others are very basic
\r
184 {name: 'Author', mapping: 'ItemAttributes > Author'},
\r
185 'Title', 'Manufacturer', 'ProductGroup'
\r
188 * </code></pre></p>
\r
189 * <p>This store is configured to consume a returned object of the form:<pre><code>
\r
190 <?xml version="1.0" encoding="UTF-8"?>
\r
191 <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-05-15">
\r
194 <IsValid>True</IsValid>
\r
195 <ItemSearchRequest>
\r
196 <Author>Sidney Sheldon</Author>
\r
197 <SearchIndex>Books</SearchIndex>
\r
198 </ItemSearchRequest>
\r
200 <TotalResults>203</TotalResults>
\r
201 <TotalPages>21</TotalPages>
\r
203 <ASIN>0446355453</ASIN>
\r
205 http://www.amazon.com/
\r
206 </DetailPageURL>
\r
207 <ItemAttributes>
\r
208 <Author>Sidney Sheldon</Author>
\r
209 <Manufacturer>Warner Books</Manufacturer>
\r
210 <ProductGroup>Book</ProductGroup>
\r
211 <Title>Master of the Game</Title>
\r
212 </ItemAttributes>
\r
215 </ItemSearchResponse>
\r
217 * An object literal of this form could also be used as the {@link #data} config option.</p>
\r
218 * <p><b>Note:</b> Although not listed here, this class accepts all of the configuration options of
\r
219 * <b>{@link Ext.data.XmlReader XmlReader}</b>.</p>
\r
221 * @param {Object} config
\r
224 Ext.data.XmlStore = Ext.extend(Ext.data.Store, {
\r
226 * @cfg {Ext.data.DataReader} reader @hide
\r
228 constructor: function(config){
\r
229 Ext.data.XmlStore.superclass.constructor.call(this, Ext.apply(config, {
\r
230 reader: new Ext.data.XmlReader(config)
\r
234 Ext.reg('xmlstore', Ext.data.XmlStore);