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.

271 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-XmlReader-method-constructor'><span id='Ext-data-XmlReader-cfg-idPath'><span id='Ext-data-XmlReader-cfg-successProperty'><span id='Ext-data-XmlReader-cfg-record'><span id='Ext-data-XmlReader-cfg-totalProperty'><span id='Ext-data-XmlReader'>/**
  19. </span></span></span></span></span></span> * @class Ext.data.XmlReader
  20. * @extends Ext.data.DataReader
  21. * &lt;p&gt;Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
  22. * based on mappings in a provided {@link Ext.data.Record} constructor.&lt;/p&gt;
  23. * &lt;p&gt;&lt;b&gt;Note&lt;/b&gt;: that in order for the browser to parse a returned XML document, the Content-Type
  24. * header in the HTTP response must be set to &quot;text/xml&quot; or &quot;application/xml&quot;.&lt;/p&gt;
  25. * &lt;p&gt;Example code:&lt;/p&gt;
  26. * &lt;pre&gt;&lt;code&gt;
  27. var Employee = Ext.data.Record.create([
  28. {name: 'name', mapping: 'name'}, // &quot;mapping&quot; property not needed if it is the same as &quot;name&quot;
  29. {name: 'occupation'} // This field will use &quot;occupation&quot; as the mapping.
  30. ]);
  31. var myReader = new Ext.data.XmlReader({
  32. totalProperty: &quot;results&quot;, // The element which contains the total dataset size (optional)
  33. record: &quot;row&quot;, // The repeated element which contains row information
  34. idProperty: &quot;id&quot; // The element within the row that provides an ID for the record (optional)
  35. messageProperty: &quot;msg&quot; // The element within the response that provides a user-feedback message (optional)
  36. }, Employee);
  37. &lt;/code&gt;&lt;/pre&gt;
  38. * &lt;p&gt;
  39. * This would consume an XML file like this:
  40. * &lt;pre&gt;&lt;code&gt;
  41. &amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  42. &amp;lt;dataset&gt;
  43. &amp;lt;results&gt;2&amp;lt;/results&gt;
  44. &amp;lt;row&gt;
  45. &amp;lt;id&gt;1&amp;lt;/id&gt;
  46. &amp;lt;name&gt;Bill&amp;lt;/name&gt;
  47. &amp;lt;occupation&gt;Gardener&amp;lt;/occupation&gt;
  48. &amp;lt;/row&gt;
  49. &amp;lt;row&gt;
  50. &amp;lt;id&gt;2&amp;lt;/id&gt;
  51. &amp;lt;name&gt;Ben&amp;lt;/name&gt;
  52. &amp;lt;occupation&gt;Horticulturalist&amp;lt;/occupation&gt;
  53. &amp;lt;/row&gt;
  54. &amp;lt;/dataset&gt;
  55. &lt;/code&gt;&lt;/pre&gt;
  56. * @cfg {String} totalProperty The DomQuery path from which to retrieve the total number of records
  57. * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
  58. * paged from the remote server.
  59. * @cfg {String} record The DomQuery path to the repeated element which contains record information.
  60. * @cfg {String} successProperty The DomQuery path to the success attribute used by forms.
  61. * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
  62. * a record identifier value.
  63. * @constructor
  64. * Create a new XmlReader.
  65. * @param {Object} meta Metadata configuration options
  66. * @param {Object} recordType Either an Array of field definition objects as passed to
  67. * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
  68. */
  69. Ext.data.XmlReader = function(meta, recordType){
  70. meta = meta || {};
  71. // backwards compat, convert idPath or id / success
  72. Ext.applyIf(meta, {
  73. idProperty: meta.idProperty || meta.idPath || meta.id,
  74. successProperty: meta.successProperty || meta.success
  75. });
  76. Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
  77. };
  78. Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
  79. <span id='Ext-data-XmlReader-method-read'> /**
  80. </span> * This method is only used by a DataProxy which has retrieved data from a remote server.
  81. * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
  82. * to contain a property called &lt;tt&gt;responseXML&lt;/tt&gt; which refers to an XML document object.
  83. * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
  84. * a cache of Ext.data.Records.
  85. */
  86. read : function(response){
  87. var doc = response.responseXML;
  88. if(!doc) {
  89. throw {message: &quot;XmlReader.read: XML Document not available&quot;};
  90. }
  91. return this.readRecords(doc);
  92. },
  93. <span id='Ext-data-XmlReader-method-readRecords'> /**
  94. </span> * Create a data block containing Ext.data.Records from an XML document.
  95. * @param {Object} doc A parsed XML document.
  96. * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
  97. * a cache of Ext.data.Records.
  98. */
  99. readRecords : function(doc){
  100. <span id='Ext-data-XmlReader-property-xmlData'> /**
  101. </span> * After any data loads/reads, the raw XML Document is available for further custom processing.
  102. * @type XMLDocument
  103. */
  104. this.xmlData = doc;
  105. var root = doc.documentElement || doc,
  106. q = Ext.DomQuery,
  107. totalRecords = 0,
  108. success = true;
  109. if(this.meta.totalProperty){
  110. totalRecords = this.getTotal(root, 0);
  111. }
  112. if(this.meta.successProperty){
  113. success = this.getSuccess(root);
  114. }
  115. var records = this.extractData(q.select(this.meta.record, root), true); // &lt;-- true to return Ext.data.Record[]
  116. // TODO return Ext.data.Response instance. @see #readResponse
  117. return {
  118. success : success,
  119. records : records,
  120. totalRecords : totalRecords || records.length
  121. };
  122. },
  123. <span id='Ext-data-XmlReader-method-readResponse'> /**
  124. </span> * Decode an XML response from server.
  125. * @param {String} action [{@link Ext.data.Api#actions} create|read|update|destroy]
  126. * @param {Object} response HTTP Response object from browser.
  127. * @return {Ext.data.Response} An instance of {@link Ext.data.Response}
  128. */
  129. readResponse : function(action, response) {
  130. var q = Ext.DomQuery,
  131. doc = response.responseXML,
  132. root = doc.documentElement || doc;
  133. // create general Response instance.
  134. var res = new Ext.data.Response({
  135. action: action,
  136. success : this.getSuccess(root),
  137. message: this.getMessage(root),
  138. data: this.extractData(q.select(this.meta.record, root) || q.select(this.meta.root, root), false),
  139. raw: doc
  140. });
  141. if (Ext.isEmpty(res.success)) {
  142. throw new Ext.data.DataReader.Error('successProperty-response', this.meta.successProperty);
  143. }
  144. // Create actions from a response having status 200 must return pk
  145. if (action === Ext.data.Api.actions.create) {
  146. var def = Ext.isDefined(res.data);
  147. if (def &amp;&amp; Ext.isEmpty(res.data)) {
  148. throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
  149. }
  150. else if (!def) {
  151. throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
  152. }
  153. }
  154. return res;
  155. },
  156. getSuccess : function() {
  157. return true;
  158. },
  159. <span id='global-method-buildExtractors'> /**
  160. </span> * build response-data extractor functions.
  161. * @private
  162. * @ignore
  163. */
  164. buildExtractors : function() {
  165. if(this.ef){
  166. return;
  167. }
  168. var s = this.meta,
  169. Record = this.recordType,
  170. f = Record.prototype.fields,
  171. fi = f.items,
  172. fl = f.length;
  173. if(s.totalProperty) {
  174. this.getTotal = this.createAccessor(s.totalProperty);
  175. }
  176. if(s.successProperty) {
  177. this.getSuccess = this.createAccessor(s.successProperty);
  178. }
  179. if (s.messageProperty) {
  180. this.getMessage = this.createAccessor(s.messageProperty);
  181. }
  182. this.getRoot = function(res) {
  183. return (!Ext.isEmpty(res[this.meta.record])) ? res[this.meta.record] : res[this.meta.root];
  184. };
  185. if (s.idPath || s.idProperty) {
  186. var g = this.createAccessor(s.idPath || s.idProperty);
  187. this.getId = function(rec) {
  188. var id = g(rec) || rec.id;
  189. return (id === undefined || id === '') ? null : id;
  190. };
  191. } else {
  192. this.getId = function(){return null;};
  193. }
  194. var ef = [];
  195. for(var i = 0; i &lt; fl; i++){
  196. f = fi[i];
  197. var map = (f.mapping !== undefined &amp;&amp; f.mapping !== null) ? f.mapping : f.name;
  198. ef.push(this.createAccessor(map));
  199. }
  200. this.ef = ef;
  201. },
  202. <span id='global-method-createAccessor'> /**
  203. </span> * Creates a function to return some particular key of data from a response.
  204. * @param {String} key
  205. * @return {Function}
  206. * @private
  207. * @ignore
  208. */
  209. createAccessor : function(){
  210. var q = Ext.DomQuery;
  211. return function(key) {
  212. if (Ext.isFunction(key)) {
  213. return key;
  214. }
  215. switch(key) {
  216. case this.meta.totalProperty:
  217. return function(root, def){
  218. return q.selectNumber(key, root, def);
  219. };
  220. break;
  221. case this.meta.successProperty:
  222. return function(root, def) {
  223. var sv = q.selectValue(key, root, true);
  224. var success = sv !== false &amp;&amp; sv !== 'false';
  225. return success;
  226. };
  227. break;
  228. default:
  229. return function(root, def) {
  230. return q.selectValue(key, root, def);
  231. };
  232. break;
  233. }
  234. };
  235. }(),
  236. <span id='global-method-extractValues'> /**
  237. </span> * extracts values and type-casts a row of data from server, extracted by #extractData
  238. * @param {Hash} data
  239. * @param {Ext.data.Field[]} items
  240. * @param {Number} len
  241. * @private
  242. * @ignore
  243. */
  244. extractValues : function(data, items, len) {
  245. var f, values = {};
  246. for(var j = 0; j &lt; len; j++){
  247. f = items[j];
  248. var v = this.ef[j](data);
  249. values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
  250. }
  251. return values;
  252. }
  253. });</pre>
  254. </body>
  255. </html>