Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / data / DataReader.js
1 /*!
2  * Ext JS Library 3.0.0
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 };\r
37 \r
38 Ext.data.DataReader.prototype = {\r
39 \r
40     /**\r
41      * Abstract method, overridden in {@link Ext.data.JsonReader}\r
42      */\r
43     buildExtractors : Ext.emptyFn,\r
44 \r
45     /**\r
46      * Used for un-phantoming a record after a successful database insert.  Sets the records pk along with new data from server.\r
47      * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration.  The incoming\r
48      * data from server will be merged with the data in the local record.\r
49      * In addition, you <b>must</b> return record-data from the server in the same order received.\r
50      * Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be suppressed.\r
51      * @param {Record/Record[]} record The phantom record to be realized.\r
52      * @param {Object/Object[]} data The new record data to apply.  Must include the primary-key from database defined in idProperty field.\r
53      */\r
54     realize: function(rs, data){\r
55         if (Ext.isArray(rs)) {\r
56             for (var i = rs.length - 1; i >= 0; i--) {\r
57                 // recurse\r
58                 if (Ext.isArray(data)) {\r
59                     this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());\r
60                 }\r
61                 else {\r
62                     // weird...rs is an array but data isn't??  recurse but just send in the whole invalid data object.\r
63                     // the else clause below will detect !this.isData and throw exception.\r
64                     this.realize(rs.splice(i,1).shift(), data);\r
65                 }\r
66             }\r
67         }\r
68         else {\r
69             // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.\r
70             if (Ext.isArray(data) && data.length == 1) {\r
71                 data = data.shift();\r
72             }\r
73             if (!this.isData(data)) {\r
74                 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.\r
75                 //rs.commit();\r
76                 throw new Ext.data.DataReader.Error('realize', rs);\r
77             }\r
78             this.buildExtractors();\r
79             var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);\r
80             rs.phantom = false; // <-- That's what it's all about\r
81             rs._phid = rs.id;  // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords\r
82             rs.id = data[this.meta.idProperty];\r
83             rs.data = values;\r
84             rs.commit();\r
85         }\r
86     },\r
87 \r
88     /**\r
89      * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.\r
90      * You <b>must</b> return a complete new record from the server.  If you don't, your local record's missing fields\r
91      * will be populated with the default values specified in your Ext.data.Record.create specification.  Without a defaultValue,\r
92      * local fields will be populated with empty string "".  So return your entire record's data after both remote create and update.\r
93      * In addition, you <b>must</b> return record-data from the server in the same order received.\r
94      * Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be suppressed as the record receives\r
95      * a 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                 // TODO: create custom Exception class to return record in thrown exception.  Allow exception-handler the choice\r
119                 // to commit or not rather than blindly rs.commit() here.\r
120                 rs.commit();\r
121                 throw new Ext.data.DataReader.Error('update', rs);\r
122             }\r
123             this.buildExtractors();\r
124             rs.data = this.extractValues(Ext.apply(rs.data, data), rs.fields.items, rs.fields.items.length);\r
125             rs.commit();\r
126         }\r
127     },\r
128 \r
129     /**\r
130      * Returns true if the supplied data-hash <b>looks</b> and quacks like data.  Checks to see if it has a key\r
131      * corresponding to idProperty defined in your DataReader config containing non-empty pk.\r
132      * @param {Object} data\r
133      * @return {Boolean}\r
134      */\r
135     isData : function(data) {\r
136         return (data && Ext.isObject(data) && !Ext.isEmpty(data[this.meta.idProperty])) ? true : false;\r
137     }\r
138 };\r
139 \r
140 /**\r
141  * @class Ext.data.DataReader.Error\r
142  * @extends Ext.Error\r
143  * General error class for Ext.data.DataReader\r
144  */\r
145 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {\r
146     constructor : function(message, arg) {\r
147         this.arg = arg;\r
148         Ext.Error.call(this, message);\r
149     },\r
150     name: 'Ext.data.DataReader'\r
151 });\r
152 Ext.apply(Ext.data.DataReader.Error.prototype, {\r
153     lang : {\r
154         'update': "#update received invalid data from server.  Please see docs for DataReader#update and review your DataReader configuration.",\r
155         'realize': "#realize was called with invalid remote-data.  Please see the docs for DataReader#realize and review your DataReader configuration.",\r
156         'invalid-response': "#readResponse received an invalid response from the server."\r
157     }\r
158 });\r
159 \r
160 \r