Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / data / DataReader.js
diff --git a/src/data/DataReader.js b/src/data/DataReader.js
new file mode 100644 (file)
index 0000000..d9329d6
--- /dev/null
@@ -0,0 +1,160 @@
+/*!
+ * Ext JS Library 3.0.0
+ * Copyright(c) 2006-2009 Ext JS, LLC
+ * licensing@extjs.com
+ * http://www.extjs.com/license
+ */
+/**\r
+ * @class Ext.data.DataReader\r
+ * Abstract base class for reading structured data from a data source and converting\r
+ * it into an object containing {@link Ext.data.Record} objects and metadata for use\r
+ * by an {@link Ext.data.Store}.  This class is intended to be extended and should not\r
+ * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},\r
+ * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.\r
+ * @constructor Create a new DataReader\r
+ * @param {Object} meta Metadata configuration options (implementation-specific).\r
+ * @param {Array/Object} recordType\r
+ * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which\r
+ * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}\r
+ * constructor created using {@link Ext.data.Record#create}.</p>\r
+ */\r
+Ext.data.DataReader = function(meta, recordType){\r
+    /**\r
+     * This DataReader's configured metadata as passed to the constructor.\r
+     * @type Mixed\r
+     * @property meta\r
+     */\r
+    this.meta = meta;\r
+    /**\r
+     * @cfg {Array/Object} fields\r
+     * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which\r
+     * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}\r
+     * constructor created from {@link Ext.data.Record#create}.</p>\r
+     */\r
+    this.recordType = Ext.isArray(recordType) ?\r
+        Ext.data.Record.create(recordType) : recordType;\r
+};\r
+\r
+Ext.data.DataReader.prototype = {\r
+\r
+    /**\r
+     * Abstract method, overridden in {@link Ext.data.JsonReader}\r
+     */\r
+    buildExtractors : Ext.emptyFn,\r
+\r
+    /**\r
+     * Used for un-phantoming a record after a successful database insert.  Sets the records pk along with new data from server.\r
+     * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration.  The incoming\r
+     * data from server will be merged with the data in the local record.\r
+     * In addition, you <b>must</b> return record-data from the server in the same order received.\r
+     * Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be suppressed.\r
+     * @param {Record/Record[]} record The phantom record to be realized.\r
+     * @param {Object/Object[]} data The new record data to apply.  Must include the primary-key from database defined in idProperty field.\r
+     */\r
+    realize: function(rs, data){\r
+        if (Ext.isArray(rs)) {\r
+            for (var i = rs.length - 1; i >= 0; i--) {\r
+                // recurse\r
+                if (Ext.isArray(data)) {\r
+                    this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());\r
+                }\r
+                else {\r
+                    // weird...rs is an array but data isn't??  recurse but just send in the whole invalid data object.\r
+                    // the else clause below will detect !this.isData and throw exception.\r
+                    this.realize(rs.splice(i,1).shift(), data);\r
+                }\r
+            }\r
+        }\r
+        else {\r
+            // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.\r
+            if (Ext.isArray(data) && data.length == 1) {\r
+                data = data.shift();\r
+            }\r
+            if (!this.isData(data)) {\r
+                // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.\r
+                //rs.commit();\r
+                throw new Ext.data.DataReader.Error('realize', rs);\r
+            }\r
+            this.buildExtractors();\r
+            var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);\r
+            rs.phantom = false; // <-- That's what it's all about\r
+            rs._phid = rs.id;  // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords\r
+            rs.id = data[this.meta.idProperty];\r
+            rs.data = values;\r
+            rs.commit();\r
+        }\r
+    },\r
+\r
+    /**\r
+     * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.\r
+     * You <b>must</b> return a complete new record from the server.  If you don't, your local record's missing fields\r
+     * will be populated with the default values specified in your Ext.data.Record.create specification.  Without a defaultValue,\r
+     * local fields will be populated with empty string "".  So return your entire record's data after both remote create and update.\r
+     * In addition, you <b>must</b> return record-data from the server in the same order received.\r
+     * Will perform a commit as well, un-marking dirty-fields.  Store's "update" event will be suppressed as the record receives\r
+     * a fresh new data-hash.\r
+     * @param {Record/Record[]} rs\r
+     * @param {Object/Object[]} data\r
+     */\r
+    update : function(rs, data) {\r
+        if (Ext.isArray(rs)) {\r
+            for (var i=rs.length-1; i >= 0; i--) {\r
+                if (Ext.isArray(data)) {\r
+                    this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());\r
+                }\r
+                else {\r
+                    // weird...rs is an array but data isn't??  recurse but just send in the whole data object.\r
+                    // the else clause below will detect !this.isData and throw exception.\r
+                    this.update(rs.splice(i,1).shift(), data);\r
+                }\r
+            }\r
+        }\r
+        else {\r
+                     // If rs is NOT an array but data IS, see if data contains just 1 record.  If so extract it and carry on.\r
+            if (Ext.isArray(data) && data.length == 1) {\r
+                data = data.shift();\r
+            }\r
+            if (!this.isData(data)) {\r
+                // TODO: create custom Exception class to return record in thrown exception.  Allow exception-handler the choice\r
+                // to commit or not rather than blindly rs.commit() here.\r
+                rs.commit();\r
+                throw new Ext.data.DataReader.Error('update', rs);\r
+            }\r
+            this.buildExtractors();\r
+            rs.data = this.extractValues(Ext.apply(rs.data, data), rs.fields.items, rs.fields.items.length);\r
+            rs.commit();\r
+        }\r
+    },\r
+\r
+    /**\r
+     * Returns true if the supplied data-hash <b>looks</b> and quacks like data.  Checks to see if it has a key\r
+     * corresponding to idProperty defined in your DataReader config containing non-empty pk.\r
+     * @param {Object} data\r
+     * @return {Boolean}\r
+     */\r
+    isData : function(data) {\r
+        return (data && Ext.isObject(data) && !Ext.isEmpty(data[this.meta.idProperty])) ? true : false;\r
+    }\r
+};\r
+\r
+/**\r
+ * @class Ext.data.DataReader.Error\r
+ * @extends Ext.Error\r
+ * General error class for Ext.data.DataReader\r
+ */\r
+Ext.data.DataReader.Error = Ext.extend(Ext.Error, {\r
+    constructor : function(message, arg) {\r
+        this.arg = arg;\r
+        Ext.Error.call(this, message);\r
+    },\r
+    name: 'Ext.data.DataReader'\r
+});\r
+Ext.apply(Ext.data.DataReader.Error.prototype, {\r
+    lang : {\r
+        'update': "#update received invalid data from server.  Please see docs for DataReader#update and review your DataReader configuration.",\r
+        'realize': "#realize was called with invalid remote-data.  Please see the docs for DataReader#realize and review your DataReader configuration.",\r
+        'invalid-response': "#readResponse received an invalid response from the server."\r
+    }\r
+});\r
+\r
+\r