Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / DataReader.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataReader"></div>/**\r
9  * @class Ext.data.DataReader\r
10  * Abstract base class for reading structured data from a data source and converting\r
11  * it into an object containing {@link Ext.data.Record} objects and metadata for use\r
12  * by an {@link Ext.data.Store}.  This class is intended to be extended and should not\r
13  * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},\r
14  * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.\r
15  * @constructor Create a new DataReader\r
16  * @param {Object} meta Metadata configuration options (implementation-specific).\r
17  * @param {Array/Object} recordType\r
18  * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which\r
19  * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}\r
20  * constructor created using {@link Ext.data.Record#create}.</p>\r
21  */\r
22 Ext.data.DataReader = function(meta, recordType){\r
23     <div id="prop-Ext.data.DataReader-meta"></div>/**\r
24      * This DataReader's configured metadata as passed to the constructor.\r
25      * @type Mixed\r
26      * @property meta\r
27      */\r
28     this.meta = meta;\r
29     <div id="cfg-Ext.data.DataReader-fields"></div>/**\r
30      * @cfg {Array/Object} fields\r
31      * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which\r
32      * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}\r
33      * constructor created from {@link Ext.data.Record#create}.</p>\r
34      */\r
35     this.recordType = Ext.isArray(recordType) ?\r
36         Ext.data.Record.create(recordType) : recordType;\r
37 \r
38     // make sure extraction functions are defined.\r
39     this.buildExtractors();\r
40 };\r
41 \r
42 Ext.data.DataReader.prototype = {\r
43 \r
44     <div id="prop-Ext.data.DataReader-buildExtractors"></div>/**\r
45      * Abstract method, overridden in {@link Ext.data.JsonReader}\r
46      */\r
47     buildExtractors : Ext.emptyFn,\r
48 \r
49     <div id="method-Ext.data.DataReader-realize"></div>/**\r
50      * Used for un-phantoming a record after a successful database insert.  Sets the records pk along with new data from server.\r
51      * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration.  The incoming\r
52      * data from server will be merged with the data in the local record.\r
53      * In addition, you <b>must</b> return record-data from the server in the same order received.\r
54      * Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be suppressed.\r
55      * @param {Record/Record[]} record The phantom record to be realized.\r
56      * @param {Object/Object[]} data The new record data to apply.  Must include the primary-key from database defined in idProperty field.\r
57      */\r
58     realize: function(rs, data){\r
59         if (Ext.isArray(rs)) {\r
60             for (var i = rs.length - 1; i >= 0; i--) {\r
61                 // recurse\r
62                 if (Ext.isArray(data)) {\r
63                     this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());\r
64                 }\r
65                 else {\r
66                     // weird...rs is an array but data isn't??  recurse but just send in the whole invalid data object.\r
67                     // the else clause below will detect !this.isData and throw exception.\r
68                     this.realize(rs.splice(i,1).shift(), data);\r
69                 }\r
70             }\r
71         }\r
72         else {\r
73             // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.\r
74             if (Ext.isArray(data) && data.length == 1) {\r
75                 data = data.shift();\r
76             }\r
77             if (!this.isData(data)) {\r
78                 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.\r
79                 //rs.commit();\r
80                 throw new Ext.data.DataReader.Error('realize', rs);\r
81             }\r
82             var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);\r
83             rs.phantom = false; // <-- That's what it's all about\r
84             rs._phid = rs.id;  // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords\r
85             rs.id = data[this.meta.idProperty];\r
86             rs.data = values;\r
87             rs.commit();\r
88         }\r
89     },\r
90 \r
91     <div id="method-Ext.data.DataReader-update"></div>/**\r
92      * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.\r
93      * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in\r
94      * the same order received.  Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be\r
95      * suppressed as the record receives fresh new data-hash\r
96      * @param {Record/Record[]} rs\r
97      * @param {Object/Object[]} data\r
98      */\r
99     update : function(rs, data) {\r
100         if (Ext.isArray(rs)) {\r
101             for (var i=rs.length-1; i >= 0; i--) {\r
102                 if (Ext.isArray(data)) {\r
103                     this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());\r
104                 }\r
105                 else {\r
106                     // weird...rs is an array but data isn't??  recurse but just send in the whole data object.\r
107                     // the else clause below will detect !this.isData and throw exception.\r
108                     this.update(rs.splice(i,1).shift(), data);\r
109                 }\r
110             }\r
111         }\r
112         else {\r
113             // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.\r
114             if (Ext.isArray(data) && data.length == 1) {\r
115                 data = data.shift();\r
116             }\r
117             if (this.isData(data)) {\r
118                 rs.data = this.extractValues(Ext.apply(rs.data, data), rs.fields.items, rs.fields.items.length);\r
119             }\r
120             rs.commit();\r
121         }\r
122     },\r
123 \r
124     <div id="method-Ext.data.DataReader-isData"></div>/**\r
125      * Returns true if the supplied data-hash <b>looks</b> and quacks like data.  Checks to see if it has a key\r
126      * corresponding to idProperty defined in your DataReader config containing non-empty pk.\r
127      * @param {Object} data\r
128      * @return {Boolean}\r
129      */\r
130     isData : function(data) {\r
131         return (data && Ext.isObject(data) && !Ext.isEmpty(data[this.meta.idProperty])) ? true : false;\r
132     }\r
133 };\r
134 \r
135 <div id="cls-Ext.data.DataReader.Error"></div>/**\r
136  * @class Ext.data.DataReader.Error\r
137  * @extends Ext.Error\r
138  * General error class for Ext.data.DataReader\r
139  */\r
140 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {\r
141     constructor : function(message, arg) {\r
142         this.arg = arg;\r
143         Ext.Error.call(this, message);\r
144     },\r
145     name: 'Ext.data.DataReader'\r
146 });\r
147 Ext.apply(Ext.data.DataReader.Error.prototype, {\r
148     lang : {\r
149         'update': "#update received invalid data from server.  Please see docs for DataReader#update and review your DataReader configuration.",\r
150         'realize': "#realize was called with invalid remote-data.  Please see the docs for DataReader#realize and review your DataReader configuration.",\r
151         'invalid-response': "#readResponse received an invalid response from the server."\r
152     }\r
153 });\r
154 \r
155 \r
156 </pre>    \r
157 </body>\r
158 </html>