+
The source code
@@ -34,17 +35,49 @@ Ext.data.DataReader = function(meta, recordType){
*/
this.recordType = Ext.isArray(recordType) ?
Ext.data.Record.create(recordType) : recordType;
-
- // make sure extraction functions are defined.
- this.buildExtractors();
+
+ // if recordType defined make sure extraction functions are defined
+ if (this.recordType){
+ this.buildExtractors();
+ }
};
Ext.data.DataReader.prototype = {
-
+ /**
+ * @cfg {String} messageProperty [undefined] Optional name of a property within a server-response that represents a user-feedback message.
+ */
+ /**
+ * Abstract method created in extension's buildExtractors impl.
+ */
+ getTotal: Ext.emptyFn,
+ /**
+ * Abstract method created in extension's buildExtractors impl.
+ */
+ getRoot: Ext.emptyFn,
+ /**
+ * Abstract method created in extension's buildExtractors impl.
+ */
+ getMessage: Ext.emptyFn,
+ /**
+ * Abstract method created in extension's buildExtractors impl.
+ */
+ getSuccess: Ext.emptyFn,
+ /**
+ * Abstract method created in extension's buildExtractors impl.
+ */
+ getId: Ext.emptyFn,
/**
- * Abstract method, overridden in {@link Ext.data.JsonReader}
+ * Abstract method, overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
*/
buildExtractors : Ext.emptyFn,
+ /**
+ * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
+ */
+ extractData : Ext.emptyFn,
+ /**
+ * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
+ */
+ extractValues : Ext.emptyFn,
/**
* Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
@@ -79,11 +112,15 @@ Ext.data.DataReader.prototype = {
//rs.commit();
throw new Ext.data.DataReader.Error('realize', rs);
}
- var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);
rs.phantom = false; // <-- That's what it's all about
rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
- rs.id = data[this.meta.idProperty];
- rs.data = values;
+ rs.id = this.getId(data);
+
+ rs.fields.each(function(f) {
+ if (data[f.name] !== f.defaultValue) {
+ rs.data[f.name] = data[f.name];
+ }
+ });
rs.commit();
}
},
@@ -115,12 +152,56 @@ Ext.data.DataReader.prototype = {
data = data.shift();
}
if (this.isData(data)) {
- rs.data = this.extractValues(Ext.apply(rs.data, data), rs.fields.items, rs.fields.items.length);
+ rs.fields.each(function(f) {
+ if (data[f.name] !== f.defaultValue) {
+ rs.data[f.name] = data[f.name];
+ }
+ });
}
rs.commit();
}
},
+ /**
+ * returns extracted, type-cast rows of data. Iterates to call #extractValues for each row
+ * @param {Object[]/Object} data-root from server response
+ * @param {Boolean} returnRecords [false] Set true to return instances of Ext.data.Record
+ * @private
+ */
+ extractData : function(root, returnRecords) {
+ // A bit ugly this, too bad the Record's raw data couldn't be saved in a common property named "raw" or something.
+ var rawName = (this instanceof Ext.data.JsonReader) ? 'json' : 'node';
+
+ var rs = [];
+
+ // Had to add Check for XmlReader, #isData returns true if root is an Xml-object. Want to check in order to re-factor
+ // #extractData into DataReader base, since the implementations are almost identical for JsonReader, XmlReader
+ if (this.isData(root) && !(this instanceof Ext.data.XmlReader)) {
+ root = [root];
+ }
+ var f = this.recordType.prototype.fields,
+ fi = f.items,
+ fl = f.length,
+ rs = [];
+ if (returnRecords === true) {
+ var Record = this.recordType;
+ for (var i = 0; i < root.length; i++) {
+ var n = root[i];
+ var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
+ record[rawName] = n; // <-- There's implementation of ugly bit, setting the raw record-data.
+ rs.push(record);
+ }
+ }
+ else {
+ for (var i = 0; i < root.length; i++) {
+ var data = this.extractValues(root[i], fi, fl);
+ data[this.meta.idProperty] = this.getId(root[i]);
+ rs.push(data);
+ }
+ }
+ return rs;
+ },
+
/**
* Returns true if the supplied data-hash looks and quacks like data. Checks to see if it has a key
* corresponding to idProperty defined in your DataReader config containing non-empty pk.
@@ -128,7 +209,15 @@ Ext.data.DataReader.prototype = {
* @return {Boolean}
*/
isData : function(data) {
- return (data && Ext.isObject(data) && !Ext.isEmpty(data[this.meta.idProperty])) ? true : false;
+ return (data && Ext.isObject(data) && !Ext.isEmpty(this.getId(data))) ? true : false;
+ },
+
+ // private function a store will createSequence upon
+ onMetaChange : function(meta){
+ delete this.ef;
+ this.meta = meta;
+ this.recordType = Ext.data.Record.create(meta.fields);
+ this.buildExtractors();
}
};
@@ -151,8 +240,6 @@ Ext.apply(Ext.data.DataReader.Error.prototype, {
'invalid-response': "#readResponse received an invalid response from the server."
}
});
-
-
\ No newline at end of file