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