Json2.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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-reader-Json'>/**
  19. </span> * @author Ed Spencer
  20. *
  21. * The JSON Reader is used by a Proxy to read a server response that is sent back in JSON format. This usually
  22. * happens as a result of loading a Store - for example we might create something like this:
  23. *
  24. * Ext.define('User', {
  25. * extend: 'Ext.data.Model',
  26. * fields: ['id', 'name', 'email']
  27. * });
  28. *
  29. * var store = Ext.create('Ext.data.Store', {
  30. * model: 'User',
  31. * proxy: {
  32. * type: 'ajax',
  33. * url : 'users.json',
  34. * reader: {
  35. * type: 'json'
  36. * }
  37. * }
  38. * });
  39. *
  40. * The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're
  41. * not already familiar with them.
  42. *
  43. * We created the simplest type of JSON Reader possible by simply telling our {@link Ext.data.Store Store}'s
  44. * {@link Ext.data.proxy.Proxy Proxy} that we want a JSON Reader. The Store automatically passes the configured model to the
  45. * Store, so it is as if we passed this instead:
  46. *
  47. * reader: {
  48. * type : 'json',
  49. * model: 'User'
  50. * }
  51. *
  52. * The reader we set up is ready to read data from our server - at the moment it will accept a response like this:
  53. *
  54. * [
  55. * {
  56. * &quot;id&quot;: 1,
  57. * &quot;name&quot;: &quot;Ed Spencer&quot;,
  58. * &quot;email&quot;: &quot;ed@sencha.com&quot;
  59. * },
  60. * {
  61. * &quot;id&quot;: 2,
  62. * &quot;name&quot;: &quot;Abe Elias&quot;,
  63. * &quot;email&quot;: &quot;abe@sencha.com&quot;
  64. * }
  65. * ]
  66. *
  67. * ## Reading other JSON formats
  68. *
  69. * If you already have your JSON format defined and it doesn't look quite like what we have above, you can usually
  70. * pass JsonReader a couple of configuration options to make it parse your format. For example, we can use the
  71. * {@link #cfg-root} configuration to parse data that comes back like this:
  72. *
  73. * {
  74. * &quot;users&quot;: [
  75. * {
  76. * &quot;id&quot;: 1,
  77. * &quot;name&quot;: &quot;Ed Spencer&quot;,
  78. * &quot;email&quot;: &quot;ed@sencha.com&quot;
  79. * },
  80. * {
  81. * &quot;id&quot;: 2,
  82. * &quot;name&quot;: &quot;Abe Elias&quot;,
  83. * &quot;email&quot;: &quot;abe@sencha.com&quot;
  84. * }
  85. * ]
  86. * }
  87. *
  88. * To parse this we just pass in a {@link #root} configuration that matches the 'users' above:
  89. *
  90. * reader: {
  91. * type: 'json',
  92. * root: 'users'
  93. * }
  94. *
  95. * Sometimes the JSON structure is even more complicated. Document databases like CouchDB often provide metadata
  96. * around each record inside a nested structure like this:
  97. *
  98. * {
  99. * &quot;total&quot;: 122,
  100. * &quot;offset&quot;: 0,
  101. * &quot;users&quot;: [
  102. * {
  103. * &quot;id&quot;: &quot;ed-spencer-1&quot;,
  104. * &quot;value&quot;: 1,
  105. * &quot;user&quot;: {
  106. * &quot;id&quot;: 1,
  107. * &quot;name&quot;: &quot;Ed Spencer&quot;,
  108. * &quot;email&quot;: &quot;ed@sencha.com&quot;
  109. * }
  110. * }
  111. * ]
  112. * }
  113. *
  114. * In the case above the record data is nested an additional level inside the &quot;users&quot; array as each &quot;user&quot; item has
  115. * additional metadata surrounding it ('id' and 'value' in this case). To parse data out of each &quot;user&quot; item in the
  116. * JSON above we need to specify the {@link #record} configuration like this:
  117. *
  118. * reader: {
  119. * type : 'json',
  120. * root : 'users',
  121. * record: 'user'
  122. * }
  123. *
  124. * ## Response MetaData
  125. *
  126. * The server can return metadata in its response, in addition to the record data, that describe attributes
  127. * of the data set itself or are used to reconfigure the Reader. To pass metadata in the response you simply
  128. * add a `metaData` attribute to the root of the response data. The metaData attribute can contain anything,
  129. * but supports a specific set of properties that are handled by the Reader if they are present:
  130. *
  131. * - {@link #root}: the property name of the root response node containing the record data
  132. * - {@link #idProperty}: property name for the primary key field of the data
  133. * - {@link #totalProperty}: property name for the total number of records in the data
  134. * - {@link #successProperty}: property name for the success status of the response
  135. * - {@link #messageProperty}: property name for an optional response message
  136. * - {@link Ext.data.Model#cfg-fields fields}: Config used to reconfigure the Model's fields before converting the
  137. * response data into records
  138. *
  139. * An initial Reader configuration containing all of these properties might look like this (&quot;fields&quot; would be
  140. * included in the Model definition, not shown):
  141. *
  142. * reader: {
  143. * type : 'json',
  144. * root : 'root',
  145. * idProperty : 'id',
  146. * totalProperty : 'total',
  147. * successProperty: 'success',
  148. * messageProperty: 'message'
  149. * }
  150. *
  151. * If you were to pass a response object containing attributes different from those initially defined above, you could
  152. * use the `metaData` attribute to reconifgure the Reader on the fly. For example:
  153. *
  154. * {
  155. * &quot;count&quot;: 1,
  156. * &quot;ok&quot;: true,
  157. * &quot;msg&quot;: &quot;Users found&quot;,
  158. * &quot;users&quot;: [{
  159. * &quot;userId&quot;: 123,
  160. * &quot;name&quot;: &quot;Ed Spencer&quot;,
  161. * &quot;email&quot;: &quot;ed@sencha.com&quot;
  162. * }],
  163. * &quot;metaData&quot;: {
  164. * &quot;root&quot;: &quot;users&quot;,
  165. * &quot;idProperty&quot;: 'userId',
  166. * &quot;totalProperty&quot;: 'count',
  167. * &quot;successProperty&quot;: 'ok',
  168. * &quot;messageProperty&quot;: 'msg'
  169. * }
  170. * }
  171. *
  172. * You can also place any other arbitrary data you need into the `metaData` attribute which will be ignored by the Reader,
  173. * but will be accessible via the Reader's {@link #metaData} property (which is also passed to listeners via the Proxy's
  174. * {@link Ext.data.proxy.Proxy#metachange metachange} event (also relayed by the {@link Ext.data.AbstractStore#metachange
  175. * store}). Application code can then process the passed metadata in any way it chooses.
  176. *
  177. * A simple example for how this can be used would be customizing the fields for a Model that is bound to a grid. By passing
  178. * the `fields` property the Model will be automatically updated by the Reader internally, but that change will not be
  179. * reflected automatically in the grid unless you also update the column configuration. You could do this manually, or you
  180. * could simply pass a standard grid {@link Ext.panel.Table#columns column} config object as part of the `metaData` attribute
  181. * and then pass that along to the grid. Here's a very simple example for how that could be accomplished:
  182. *
  183. * // response format:
  184. * {
  185. * ...
  186. * &quot;metaData&quot;: {
  187. * &quot;fields&quot;: [
  188. * { &quot;name&quot;: &quot;userId&quot;, &quot;type&quot;: &quot;int&quot; },
  189. * { &quot;name&quot;: &quot;name&quot;, &quot;type&quot;: &quot;string&quot; },
  190. * { &quot;name&quot;: &quot;birthday&quot;, &quot;type&quot;: &quot;date&quot;, &quot;dateFormat&quot;: &quot;Y-j-m&quot; },
  191. * ],
  192. * &quot;columns&quot;: [
  193. * { &quot;text&quot;: &quot;User ID&quot;, &quot;dataIndex&quot;: &quot;userId&quot;, &quot;width&quot;: 40 },
  194. * { &quot;text&quot;: &quot;User Name&quot;, &quot;dataIndex&quot;: &quot;name&quot;, &quot;flex&quot;: 1 },
  195. * { &quot;text&quot;: &quot;Birthday&quot;, &quot;dataIndex&quot;: &quot;birthday&quot;, &quot;flex&quot;: 1, &quot;format&quot;: 'Y-j-m', &quot;xtype&quot;: &quot;datecolumn&quot; }
  196. * ]
  197. * }
  198. * }
  199. *
  200. * The Reader will automatically read the meta fields config and rebuild the Model based on the new fields, but to handle
  201. * the new column configuration you would need to handle the metadata within the application code. This is done simply enough
  202. * by handling the metachange event on either the store or the proxy, e.g.:
  203. *
  204. * var store = Ext.create('Ext.data.Store', {
  205. * ...
  206. * listeners: {
  207. * 'metachange': function(store, meta) {
  208. * myGrid.reconfigure(store, meta.columns);
  209. * }
  210. * }
  211. * });
  212. *
  213. */
  214. Ext.define('Ext.data.reader.Json', {
  215. extend: 'Ext.data.reader.Reader',
  216. alternateClassName: 'Ext.data.JsonReader',
  217. alias : 'reader.json',
  218. root: '',
  219. <span id='Ext-data-reader-Json-cfg-record'> /**
  220. </span> * @cfg {String} record The optional location within the JSON response that the record data itself can be found at.
  221. * See the JsonReader intro docs for more details. This is not often needed.
  222. */
  223. <span id='Ext-data-reader-Json-cfg-useSimpleAccessors'> /**
  224. </span> * @cfg {Boolean} useSimpleAccessors True to ensure that field names/mappings are treated as literals when
  225. * reading values.
  226. *
  227. * For example, by default, using the mapping &quot;foo.bar.baz&quot; will try and read a property foo from the root, then a property bar
  228. * from foo, then a property baz from bar. Setting the simple accessors to true will read the property with the name
  229. * &quot;foo.bar.baz&quot; direct from the root object.
  230. */
  231. useSimpleAccessors: false,
  232. <span id='Ext-data-reader-Json-method-readRecords'> /**
  233. </span> * Reads a JSON object and returns a ResultSet. Uses the internal getTotal and getSuccess extractors to
  234. * retrieve meta data from the response, and extractData to turn the JSON data into model instances.
  235. * @param {Object} data The raw JSON data
  236. * @return {Ext.data.ResultSet} A ResultSet containing model instances and meta data about the results
  237. */
  238. readRecords: function(data) {
  239. //this has to be before the call to super because we use the meta data in the superclass readRecords
  240. if (data.metaData) {
  241. this.onMetaChange(data.metaData);
  242. }
  243. <span id='Ext-data-reader-Json-property-jsonData'> /**
  244. </span> * @property {Object} jsonData
  245. * A copy of this.rawData.
  246. * @deprecated Will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead.
  247. */
  248. this.jsonData = data;
  249. return this.callParent([data]);
  250. },
  251. //inherit docs
  252. getResponseData: function(response) {
  253. var data, error;
  254. try {
  255. data = Ext.decode(response.responseText);
  256. return this.readRecords(data);
  257. } catch (ex) {
  258. error = new Ext.data.ResultSet({
  259. total : 0,
  260. count : 0,
  261. records: [],
  262. success: false,
  263. message: ex.message
  264. });
  265. this.fireEvent('exception', this, response, error);
  266. Ext.Logger.warn('Unable to parse the JSON returned by the server');
  267. return error;
  268. }
  269. },
  270. //inherit docs
  271. buildExtractors : function() {
  272. var me = this;
  273. me.callParent(arguments);
  274. if (me.root) {
  275. me.getRoot = me.createAccessor(me.root);
  276. } else {
  277. me.getRoot = function(root) {
  278. return root;
  279. };
  280. }
  281. },
  282. <span id='Ext-data-reader-Json-method-extractData'> /**
  283. </span> * @private
  284. * We're just preparing the data for the superclass by pulling out the record objects we want. If a {@link #record}
  285. * was specified we have to pull those out of the larger JSON object, which is most of what this function is doing
  286. * @param {Object} root The JSON root node
  287. * @return {Ext.data.Model[]} The records
  288. */
  289. extractData: function(root) {
  290. var recordName = this.record,
  291. data = [],
  292. length, i;
  293. if (recordName) {
  294. length = root.length;
  295. if (!length &amp;&amp; Ext.isObject(root)) {
  296. length = 1;
  297. root = [root];
  298. }
  299. for (i = 0; i &lt; length; i++) {
  300. data[i] = root[i][recordName];
  301. }
  302. } else {
  303. data = root;
  304. }
  305. return this.callParent([data]);
  306. },
  307. <span id='Ext-data-reader-Json-method-createAccessor'> /**
  308. </span> * @private
  309. * @method
  310. * Returns an accessor function for the given property string. Gives support for properties such as the following:
  311. *
  312. * - 'someProperty'
  313. * - 'some.property'
  314. * - 'some[&quot;property&quot;]'
  315. *
  316. * This is used by buildExtractors to create optimized extractor functions when casting raw data into model instances.
  317. */
  318. createAccessor: (function() {
  319. var re = /[\[\.]/;
  320. return function(expr) {
  321. if (Ext.isEmpty(expr)) {
  322. return Ext.emptyFn;
  323. }
  324. if (Ext.isFunction(expr)) {
  325. return expr;
  326. }
  327. if (this.useSimpleAccessors !== true) {
  328. var i = String(expr).search(re);
  329. if (i &gt;= 0) {
  330. return Ext.functionFactory('obj', 'return obj' + (i &gt; 0 ? '.' : '') + expr);
  331. }
  332. }
  333. return function(obj) {
  334. return obj[expr];
  335. };
  336. };
  337. }()),
  338. <span id='Ext-data-reader-Json-method-createFieldAccessExpression'> /**
  339. </span> * @private
  340. * @method
  341. * Returns an accessor expression for the passed Field. Gives support for properties such as the following:
  342. *
  343. * - 'someProperty'
  344. * - 'some.property'
  345. * - 'some[&quot;property&quot;]'
  346. *
  347. * This is used by buildExtractors to create optimized on extractor function which converts raw data into model instances.
  348. */
  349. createFieldAccessExpression: (function() {
  350. var re = /[\[\.]/;
  351. return function(field, fieldVarName, dataName) {
  352. var me = this,
  353. hasMap = (field.mapping !== null),
  354. map = hasMap ? field.mapping : field.name,
  355. result,
  356. operatorSearch;
  357. if (typeof map === 'function') {
  358. result = fieldVarName + '.mapping(' + dataName + ', this)';
  359. } else if (this.useSimpleAccessors === true || ((operatorSearch = String(map).search(re)) &lt; 0)) {
  360. if (!hasMap || isNaN(map)) {
  361. // If we don't provide a mapping, we may have a field name that is numeric
  362. map = '&quot;' + map + '&quot;';
  363. }
  364. result = dataName + &quot;[&quot; + map + &quot;]&quot;;
  365. } else {
  366. result = dataName + (operatorSearch &gt; 0 ? '.' : '') + map;
  367. }
  368. return result;
  369. };
  370. }())
  371. });
  372. </pre>
  373. </body>
  374. </html>