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.

373 lines
17 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-JsonReader-method-constructor'><span id='Ext-data-JsonReader'>/**
  19. </span></span> * @class Ext.data.JsonReader
  20. * @extends Ext.data.DataReader
  21. * &lt;p&gt;Data reader class to create an Array of {@link Ext.data.Record} objects
  22. * from a JSON packet based on mappings in a provided {@link Ext.data.Record}
  23. * constructor.&lt;/p&gt;
  24. * &lt;p&gt;Example code:&lt;/p&gt;
  25. * &lt;pre&gt;&lt;code&gt;
  26. var myReader = new Ext.data.JsonReader({
  27. // metadata configuration options:
  28. {@link #idProperty}: 'id'
  29. {@link #root}: 'rows',
  30. {@link #totalProperty}: 'results',
  31. {@link Ext.data.DataReader#messageProperty}: &quot;msg&quot; // The element within the response that provides a user-feedback message (optional)
  32. // the fields config option will internally create an {@link Ext.data.Record}
  33. // constructor that provides mapping for reading the record data objects
  34. {@link Ext.data.DataReader#fields fields}: [
  35. // map Record&amp;#39;s 'firstname' field to data object&amp;#39;s key of same name
  36. {name: 'name', mapping: 'firstname'},
  37. // map Record&amp;#39;s 'job' field to data object&amp;#39;s 'occupation' key
  38. {name: 'job', mapping: 'occupation'}
  39. ]
  40. });
  41. &lt;/code&gt;&lt;/pre&gt;
  42. * &lt;p&gt;This would consume a JSON data object of the form:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  43. {
  44. results: 2000, // Reader&amp;#39;s configured {@link #totalProperty}
  45. rows: [ // Reader&amp;#39;s configured {@link #root}
  46. // record data objects:
  47. { {@link #idProperty id}: 1, firstname: 'Bill', occupation: 'Gardener' },
  48. { {@link #idProperty id}: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
  49. ...
  50. ]
  51. }
  52. &lt;/code&gt;&lt;/pre&gt;
  53. * &lt;p&gt;&lt;b&gt;&lt;u&gt;Automatic configuration using metaData&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
  54. * &lt;p&gt;It is possible to change a JsonReader's metadata at any time by including
  55. * a &lt;b&gt;&lt;tt&gt;metaData&lt;/tt&gt;&lt;/b&gt; property in the JSON data object. If the JSON data
  56. * object has a &lt;b&gt;&lt;tt&gt;metaData&lt;/tt&gt;&lt;/b&gt; property, a {@link Ext.data.Store Store}
  57. * object using this Reader will reconfigure itself to use the newly provided
  58. * field definition and fire its {@link Ext.data.Store#metachange metachange}
  59. * event. The metachange event handler may interrogate the &lt;b&gt;&lt;tt&gt;metaData&lt;/tt&gt;&lt;/b&gt;
  60. * property to perform any configuration required.&lt;/p&gt;
  61. * &lt;p&gt;Note that reconfiguring a Store potentially invalidates objects which may
  62. * refer to Fields or Records which no longer exist.&lt;/p&gt;
  63. * &lt;p&gt;To use this facility you would create the JsonReader like this:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  64. var myReader = new Ext.data.JsonReader();
  65. &lt;/code&gt;&lt;/pre&gt;
  66. * &lt;p&gt;The first data packet from the server would configure the reader by
  67. * containing a &lt;b&gt;&lt;tt&gt;metaData&lt;/tt&gt;&lt;/b&gt; property &lt;b&gt;and&lt;/b&gt; the data. For
  68. * example, the JSON data object might take the form:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  69. {
  70. metaData: {
  71. &quot;{@link #idProperty}&quot;: &quot;id&quot;,
  72. &quot;{@link #root}&quot;: &quot;rows&quot;,
  73. &quot;{@link #totalProperty}&quot;: &quot;results&quot;
  74. &quot;{@link #successProperty}&quot;: &quot;success&quot;,
  75. &quot;{@link Ext.data.DataReader#fields fields}&quot;: [
  76. {&quot;name&quot;: &quot;name&quot;},
  77. {&quot;name&quot;: &quot;job&quot;, &quot;mapping&quot;: &quot;occupation&quot;}
  78. ],
  79. // used by store to set its sortInfo
  80. &quot;sortInfo&quot;:{
  81. &quot;field&quot;: &quot;name&quot;,
  82. &quot;direction&quot;: &quot;ASC&quot;
  83. },
  84. // {@link Ext.PagingToolbar paging data} (if applicable)
  85. &quot;start&quot;: 0,
  86. &quot;limit&quot;: 2,
  87. // custom property
  88. &quot;foo&quot;: &quot;bar&quot;
  89. },
  90. // Reader&amp;#39;s configured {@link #successProperty}
  91. &quot;success&quot;: true,
  92. // Reader&amp;#39;s configured {@link #totalProperty}
  93. &quot;results&quot;: 2000,
  94. // Reader&amp;#39;s configured {@link #root}
  95. // (this data simulates 2 results {@link Ext.PagingToolbar per page})
  96. &quot;rows&quot;: [ // &lt;b&gt;*Note:&lt;/b&gt; this must be an Array
  97. { &quot;id&quot;: 1, &quot;name&quot;: &quot;Bill&quot;, &quot;occupation&quot;: &quot;Gardener&quot; },
  98. { &quot;id&quot;: 2, &quot;name&quot;: &quot;Ben&quot;, &quot;occupation&quot;: &quot;Horticulturalist&quot; }
  99. ]
  100. }
  101. * &lt;/code&gt;&lt;/pre&gt;
  102. * &lt;p&gt;The &lt;b&gt;&lt;tt&gt;metaData&lt;/tt&gt;&lt;/b&gt; property in the JSON data object should contain:&lt;/p&gt;
  103. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  104. * &lt;li&gt;any of the configuration options for this class&lt;/li&gt;
  105. * &lt;li&gt;a &lt;b&gt;&lt;tt&gt;{@link Ext.data.Record#fields fields}&lt;/tt&gt;&lt;/b&gt; property which
  106. * the JsonReader will use as an argument to the
  107. * {@link Ext.data.Record#create data Record create method} in order to
  108. * configure the layout of the Records it will produce.&lt;/li&gt;
  109. * &lt;li&gt;a &lt;b&gt;&lt;tt&gt;{@link Ext.data.Store#sortInfo sortInfo}&lt;/tt&gt;&lt;/b&gt; property
  110. * which the JsonReader will use to set the {@link Ext.data.Store}'s
  111. * {@link Ext.data.Store#sortInfo sortInfo} property&lt;/li&gt;
  112. * &lt;li&gt;any custom properties needed&lt;/li&gt;
  113. * &lt;/ul&gt;&lt;/div&gt;
  114. *
  115. * @constructor
  116. * Create a new JsonReader
  117. * @param {Object} meta Metadata configuration options.
  118. * @param {Array/Object} recordType
  119. * &lt;p&gt;Either an Array of {@link Ext.data.Field Field} definition objects (which
  120. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  121. * constructor created from {@link Ext.data.Record#create}.&lt;/p&gt;
  122. */
  123. Ext.data.JsonReader = function(meta, recordType){
  124. meta = meta || {};
  125. <span id='Ext-data-JsonReader-cfg-idProperty'> /**
  126. </span> * @cfg {String} idProperty [id] Name of the property within a row object
  127. * that contains a record identifier value. Defaults to &lt;tt&gt;id&lt;/tt&gt;
  128. */
  129. <span id='Ext-data-JsonReader-cfg-successProperty'> /**
  130. </span> * @cfg {String} successProperty [success] Name of the property from which to
  131. * retrieve the success attribute. Defaults to &lt;tt&gt;success&lt;/tt&gt;. See
  132. * {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception}
  133. * for additional information.
  134. */
  135. <span id='Ext-data-JsonReader-cfg-totalProperty'> /**
  136. </span> * @cfg {String} totalProperty [total] Name of the property from which to
  137. * retrieve the total number of records in the dataset. This is only needed
  138. * if the whole dataset is not passed in one go, but is being paged from
  139. * the remote server. Defaults to &lt;tt&gt;total&lt;/tt&gt;.
  140. */
  141. <span id='Ext-data-JsonReader-cfg-root'> /**
  142. </span> * @cfg {String} root [undefined] &lt;b&gt;Required&lt;/b&gt;. The name of the property
  143. * which contains the Array of row objects. Defaults to &lt;tt&gt;undefined&lt;/tt&gt;.
  144. * An exception will be thrown if the root property is undefined. The data
  145. * packet value for this property should be an empty array to clear the data
  146. * or show no data.
  147. */
  148. Ext.applyIf(meta, {
  149. idProperty: 'id',
  150. successProperty: 'success',
  151. totalProperty: 'total'
  152. });
  153. Ext.data.JsonReader.superclass.constructor.call(this, meta, recordType || meta.fields);
  154. };
  155. Ext.extend(Ext.data.JsonReader, Ext.data.DataReader, {
  156. <span id='Ext-data-JsonReader-property-meta'> /**
  157. </span> * This JsonReader's metadata as passed to the constructor, or as passed in
  158. * the last data packet's &lt;b&gt;&lt;tt&gt;metaData&lt;/tt&gt;&lt;/b&gt; property.
  159. * @type Mixed
  160. * @property meta
  161. */
  162. <span id='Ext-data-JsonReader-method-read'> /**
  163. </span> * This method is only used by a DataProxy which has retrieved data from a remote server.
  164. * @param {Object} response The XHR object which contains the JSON data in its responseText.
  165. * @return {Object} data A data block which is used by an Ext.data.Store object as
  166. * a cache of Ext.data.Records.
  167. */
  168. read : function(response){
  169. var json = response.responseText;
  170. var o = Ext.decode(json);
  171. if(!o) {
  172. throw {message: 'JsonReader.read: Json object not found'};
  173. }
  174. return this.readRecords(o);
  175. },
  176. /*
  177. * TODO: refactor code between JsonReader#readRecords, #readResponse into 1 method.
  178. * there's ugly duplication going on due to maintaining backwards compat. with 2.0. It's time to do this.
  179. */
  180. <span id='Ext-data-JsonReader-method-readResponse'> /**
  181. </span> * Decode a JSON response from server.
  182. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  183. * @param {Object} response The XHR object returned through an Ajax server request.
  184. * @return {Response} A {@link Ext.data.Response Response} object containing the data response, and also status information.
  185. */
  186. readResponse : function(action, response) {
  187. var o = (response.responseText !== undefined) ? Ext.decode(response.responseText) : response;
  188. if(!o) {
  189. throw new Ext.data.JsonReader.Error('response');
  190. }
  191. var root = this.getRoot(o),
  192. success = this.getSuccess(o);
  193. if (success &amp;&amp; action === Ext.data.Api.actions.create) {
  194. var def = Ext.isDefined(root);
  195. if (def &amp;&amp; Ext.isEmpty(root)) {
  196. throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
  197. }
  198. else if (!def) {
  199. throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
  200. }
  201. }
  202. // instantiate response object
  203. var res = new Ext.data.Response({
  204. action: action,
  205. success: success,
  206. data: (root) ? this.extractData(root, false) : [],
  207. message: this.getMessage(o),
  208. raw: o
  209. });
  210. // blow up if no successProperty
  211. if (Ext.isEmpty(res.success)) {
  212. throw new Ext.data.JsonReader.Error('successProperty-response', this.meta.successProperty);
  213. }
  214. return res;
  215. },
  216. <span id='Ext-data-JsonReader-method-readRecords'> /**
  217. </span> * Create a data block containing Ext.data.Records from a JSON object.
  218. * @param {Object} o An object which contains an Array of row objects in the property specified
  219. * in the config as 'root, and optionally a property, specified in the config as 'totalProperty'
  220. * which contains the total size of the dataset.
  221. * @return {Object} data A data block which is used by an Ext.data.Store object as
  222. * a cache of Ext.data.Records.
  223. */
  224. readRecords : function(o){
  225. <span id='Ext-data-JsonReader-property-jsonData'> /**
  226. </span> * After any data loads, the raw JSON data is available for further custom processing. If no data is
  227. * loaded or there is a load exception this property will be undefined.
  228. * @type Object
  229. */
  230. this.jsonData = o;
  231. if(o.metaData){
  232. this.onMetaChange(o.metaData);
  233. }
  234. var s = this.meta, Record = this.recordType,
  235. f = Record.prototype.fields, fi = f.items, fl = f.length, v;
  236. var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
  237. if(s.totalProperty){
  238. v = parseInt(this.getTotal(o), 10);
  239. if(!isNaN(v)){
  240. totalRecords = v;
  241. }
  242. }
  243. if(s.successProperty){
  244. v = this.getSuccess(o);
  245. if(v === false || v === 'false'){
  246. success = false;
  247. }
  248. }
  249. // TODO return Ext.data.Response instance instead. @see #readResponse
  250. return {
  251. success : success,
  252. records : this.extractData(root, true), // &lt;-- true to return [Ext.data.Record]
  253. totalRecords : totalRecords
  254. };
  255. },
  256. // private
  257. buildExtractors : function() {
  258. if(this.ef){
  259. return;
  260. }
  261. var s = this.meta, Record = this.recordType,
  262. f = Record.prototype.fields, fi = f.items, fl = f.length;
  263. if(s.totalProperty) {
  264. this.getTotal = this.createAccessor(s.totalProperty);
  265. }
  266. if(s.successProperty) {
  267. this.getSuccess = this.createAccessor(s.successProperty);
  268. }
  269. if (s.messageProperty) {
  270. this.getMessage = this.createAccessor(s.messageProperty);
  271. }
  272. this.getRoot = s.root ? this.createAccessor(s.root) : function(p){return p;};
  273. if (s.id || s.idProperty) {
  274. var g = this.createAccessor(s.id || s.idProperty);
  275. this.getId = function(rec) {
  276. var r = g(rec);
  277. return (r === undefined || r === '') ? null : r;
  278. };
  279. } else {
  280. this.getId = function(){return null;};
  281. }
  282. var ef = [];
  283. for(var i = 0; i &lt; fl; i++){
  284. f = fi[i];
  285. var map = (f.mapping !== undefined &amp;&amp; f.mapping !== null) ? f.mapping : f.name;
  286. ef.push(this.createAccessor(map));
  287. }
  288. this.ef = ef;
  289. },
  290. <span id='global-method-simpleAccess'> /**
  291. </span> * @ignore
  292. * TODO This isn't used anywhere?? Don't we want to use this where possible instead of complex #createAccessor?
  293. */
  294. simpleAccess : function(obj, subsc) {
  295. return obj[subsc];
  296. },
  297. <span id='global-property-createAccessor'> /**
  298. </span> * @ignore
  299. */
  300. createAccessor : function(){
  301. var re = /[\[\.]/;
  302. return function(expr) {
  303. if(Ext.isEmpty(expr)){
  304. return Ext.emptyFn;
  305. }
  306. if(Ext.isFunction(expr)){
  307. return expr;
  308. }
  309. var i = String(expr).search(re);
  310. if(i &gt;= 0){
  311. return new Function('obj', 'return obj' + (i &gt; 0 ? '.' : '') + expr);
  312. }
  313. return function(obj){
  314. return obj[expr];
  315. };
  316. };
  317. }(),
  318. <span id='Ext-data-JsonReader-method-extractValues'> /**
  319. </span> * type-casts a single row of raw-data from server
  320. * @param {Object} data
  321. * @param {Array} items
  322. * @param {Integer} len
  323. * @private
  324. */
  325. extractValues : function(data, items, len) {
  326. var f, values = {};
  327. for(var j = 0; j &lt; len; j++){
  328. f = items[j];
  329. var v = this.ef[j](data);
  330. values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
  331. }
  332. return values;
  333. }
  334. });
  335. <span id='Ext-data-JsonReader-Error'>/**
  336. </span> * @class Ext.data.JsonReader.Error
  337. * Error class for JsonReader
  338. */
  339. Ext.data.JsonReader.Error = Ext.extend(Ext.Error, {
  340. <span id='Ext-data-JsonReader-Error-method-constructor'> constructor : function(message, arg) {
  341. </span> this.arg = arg;
  342. Ext.Error.call(this, message);
  343. },
  344. <span id='Ext-data-JsonReader-Error-property-name'> name : 'Ext.data.JsonReader'
  345. </span>});
  346. Ext.apply(Ext.data.JsonReader.Error.prototype, {
  347. lang: {
  348. 'response': 'An error occurred while json-decoding your server response',
  349. 'successProperty-response': 'Could not locate your &quot;successProperty&quot; in your server response. Please review your JsonReader config to ensure the config-property &quot;successProperty&quot; matches the property in your server-response. See the JsonReader docs.',
  350. 'root-undefined-config': 'Your JsonReader was configured without a &quot;root&quot; property. Please review your JsonReader config and make sure to define the root property. See the JsonReader docs.',
  351. 'idProperty-undefined' : 'Your JsonReader was configured without an &quot;idProperty&quot; Please review your JsonReader configuration and ensure the &quot;idProperty&quot; is set (e.g.: &quot;id&quot;). See the JsonReader docs.',
  352. 'root-empty': 'Data was expected to be returned by the server in the &quot;root&quot; property of the response. Please review your JsonReader configuration to ensure the &quot;root&quot; property matches that returned in the server-response. See JsonReader docs.'
  353. }
  354. });
  355. </pre>
  356. </body>
  357. </html>