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
11 KiB

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