You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
10 KiB

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