Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / docs / source / DataReader.html
1 <html>
2 <head>
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>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.3.0
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.data.DataReader"></div>/**
16  * @class Ext.data.DataReader
17  * Abstract base class for reading structured data from a data source and converting
18  * it into an object containing {@link Ext.data.Record} objects and metadata for use
19  * by an {@link Ext.data.Store}.  This class is intended to be extended and should not
20  * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
21  * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
22  * @constructor Create a new DataReader
23  * @param {Object} meta Metadata configuration options (implementation-specific).
24  * @param {Array/Object} recordType
25  * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
26  * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
27  * constructor created using {@link Ext.data.Record#create}.</p>
28  */
29 Ext.data.DataReader = function(meta, recordType){
30     <div id="prop-Ext.data.DataReader-meta"></div>/**
31      * This DataReader's configured metadata as passed to the constructor.
32      * @type Mixed
33      * @property meta
34      */
35     this.meta = meta;
36     <div id="cfg-Ext.data.DataReader-fields"></div>/**
37      * @cfg {Array/Object} fields
38      * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
39      * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
40      * constructor created from {@link Ext.data.Record#create}.</p>
41      */
42     this.recordType = Ext.isArray(recordType) ?
43         Ext.data.Record.create(recordType) : recordType;
44
45     // if recordType defined make sure extraction functions are defined
46     if (this.recordType){
47         this.buildExtractors();
48     }
49 };
50
51 Ext.data.DataReader.prototype = {
52     <div id="cfg-Ext.data.DataReader-messageProperty"></div>/**
53      * @cfg {String} messageProperty [undefined] Optional name of a property within a server-response that represents a user-feedback message.
54      */
55     <div id="prop-Ext.data.DataReader-getTotal"></div>/**
56      * Abstract method created in extension's buildExtractors impl.
57      */
58     getTotal: Ext.emptyFn,
59     <div id="prop-Ext.data.DataReader-getRoot"></div>/**
60      * Abstract method created in extension's buildExtractors impl.
61      */
62     getRoot: Ext.emptyFn,
63     <div id="prop-Ext.data.DataReader-getMessage"></div>/**
64      * Abstract method created in extension's buildExtractors impl.
65      */
66     getMessage: Ext.emptyFn,
67     <div id="prop-Ext.data.DataReader-getSuccess"></div>/**
68      * Abstract method created in extension's buildExtractors impl.
69      */
70     getSuccess: Ext.emptyFn,
71     <div id="prop-Ext.data.DataReader-getId"></div>/**
72      * Abstract method created in extension's buildExtractors impl.
73      */
74     getId: Ext.emptyFn,
75     <div id="prop-Ext.data.DataReader-buildExtractors"></div>/**
76      * Abstract method, overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
77      */
78     buildExtractors : Ext.emptyFn,
79     <div id="prop-Ext.data.DataReader-extractValues"></div>/**
80      * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
81      */
82     extractValues : Ext.emptyFn,
83
84     <div id="method-Ext.data.DataReader-realize"></div>/**
85      * Used for un-phantoming a record after a successful database insert.  Sets the records pk along with new data from server.
86      * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration.  The incoming
87      * data from server will be merged with the data in the local record.
88      * In addition, you <b>must</b> return record-data from the server in the same order received.
89      * Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be suppressed.
90      * @param {Record/Record[]} record The phantom record to be realized.
91      * @param {Object/Object[]} data The new record data to apply.  Must include the primary-key from database defined in idProperty field.
92      */
93     realize: function(rs, data){
94         if (Ext.isArray(rs)) {
95             for (var i = rs.length - 1; i >= 0; i--) {
96                 // recurse
97                 if (Ext.isArray(data)) {
98                     this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
99                 }
100                 else {
101                     // weird...rs is an array but data isn't??  recurse but just send in the whole invalid data object.
102                     // the else clause below will detect !this.isData and throw exception.
103                     this.realize(rs.splice(i,1).shift(), data);
104                 }
105             }
106         }
107         else {
108             // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.
109             if (Ext.isArray(data) && data.length == 1) {
110                 data = data.shift();
111             }
112             if (!this.isData(data)) {
113                 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
114                 //rs.commit();
115                 throw new Ext.data.DataReader.Error('realize', rs);
116             }
117             rs.phantom = false; // <-- That's what it's all about
118             rs._phid = rs.id;  // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
119             rs.id = this.getId(data);
120             rs.data = data;
121
122             rs.commit();
123         }
124     },
125
126     <div id="method-Ext.data.DataReader-update"></div>/**
127      * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
128      * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in
129      * the same order received.  Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be
130      * suppressed as the record receives fresh new data-hash
131      * @param {Record/Record[]} rs
132      * @param {Object/Object[]} data
133      */
134     update : function(rs, data) {
135         if (Ext.isArray(rs)) {
136             for (var i=rs.length-1; i >= 0; i--) {
137                 if (Ext.isArray(data)) {
138                     this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());
139                 }
140                 else {
141                     // weird...rs is an array but data isn't??  recurse but just send in the whole data object.
142                     // the else clause below will detect !this.isData and throw exception.
143                     this.update(rs.splice(i,1).shift(), data);
144                 }
145             }
146         }
147         else {
148             // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.
149             if (Ext.isArray(data) && data.length == 1) {
150                 data = data.shift();
151             }
152             if (this.isData(data)) {
153                 rs.data = Ext.apply(rs.data, data);
154             }
155             rs.commit();
156         }
157     },
158
159     /**
160      * returns extracted, type-cast rows of data.  Iterates to call #extractValues for each row
161      * @param {Object[]/Object} data-root from server response
162      * @param {Boolean} returnRecords [false] Set true to return instances of Ext.data.Record
163      * @private
164      */
165     extractData : function(root, returnRecords) {
166         // A bit ugly this, too bad the Record's raw data couldn't be saved in a common property named "raw" or something.
167         var rawName = (this instanceof Ext.data.JsonReader) ? 'json' : 'node';
168
169         var rs = [];
170
171         // Had to add Check for XmlReader, #isData returns true if root is an Xml-object.  Want to check in order to re-factor
172         // #extractData into DataReader base, since the implementations are almost identical for JsonReader, XmlReader
173         if (this.isData(root) && !(this instanceof Ext.data.XmlReader)) {
174             root = [root];
175         }
176         var f       = this.recordType.prototype.fields,
177             fi      = f.items,
178             fl      = f.length,
179             rs      = [];
180         if (returnRecords === true) {
181             var Record = this.recordType;
182             for (var i = 0; i < root.length; i++) {
183                 var n = root[i];
184                 var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
185                 record[rawName] = n;    // <-- There's implementation of ugly bit, setting the raw record-data.
186                 rs.push(record);
187             }
188         }
189         else {
190             for (var i = 0; i < root.length; i++) {
191                 var data = this.extractValues(root[i], fi, fl);
192                 data[this.meta.idProperty] = this.getId(root[i]);
193                 rs.push(data);
194             }
195         }
196         return rs;
197     },
198
199     <div id="method-Ext.data.DataReader-isData"></div>/**
200      * Returns true if the supplied data-hash <b>looks</b> and quacks like data.  Checks to see if it has a key
201      * corresponding to idProperty defined in your DataReader config containing non-empty pk.
202      * @param {Object} data
203      * @return {Boolean}
204      */
205     isData : function(data) {
206         return (data && Ext.isObject(data) && !Ext.isEmpty(this.getId(data))) ? true : false;
207     },
208
209     // private function a store will createSequence upon
210     onMetaChange : function(meta){
211         delete this.ef;
212         this.meta = meta;
213         this.recordType = Ext.data.Record.create(meta.fields);
214         this.buildExtractors();
215     }
216 };
217
218 <div id="cls-Ext.data.DataReader.Error"></div>/**
219  * @class Ext.data.DataReader.Error
220  * @extends Ext.Error
221  * General error class for Ext.data.DataReader
222  */
223 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
224     constructor : function(message, arg) {
225         this.arg = arg;
226         Ext.Error.call(this, message);
227     },
228     name: 'Ext.data.DataReader'
229 });
230 Ext.apply(Ext.data.DataReader.Error.prototype, {
231     lang : {
232         'update': "#update received invalid data from server.  Please see docs for DataReader#update and review your DataReader configuration.",
233         'realize': "#realize was called with invalid remote-data.  Please see the docs for DataReader#realize and review your DataReader configuration.",
234         'invalid-response': "#readResponse received an invalid response from the server."
235     }
236 });
237 </pre>    
238 </body>
239 </html>