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.

663 lines
26 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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.JsonWriter
  17. * @extends Ext.data.DataWriter
  18. * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action.
  19. */
  20. Ext.data.JsonWriter = Ext.extend(Ext.data.DataWriter, {
  21. /**
  22. * @cfg {Boolean} encode <p><tt>true</tt> to {@link Ext.util.JSON#encode JSON encode} the
  23. * {@link Ext.data.DataWriter#toHash hashed data} into a standard HTTP parameter named after this
  24. * Reader's <code>meta.root</code> property which, by default is imported from the associated Reader. Defaults to <tt>true</tt>.</p>
  25. * <p>If set to <code>false</code>, the hashed data is {@link Ext.util.JSON#encode JSON encoded}, along with
  26. * the associated {@link Ext.data.Store}'s {@link Ext.data.Store#baseParams baseParams}, into the POST body.</p>
  27. * <p>When using {@link Ext.data.DirectProxy}, set this to <tt>false</tt> since Ext.Direct.JsonProvider will perform
  28. * its own json-encoding. In addition, if you're using {@link Ext.data.HttpProxy}, setting to <tt>false</tt>
  29. * will cause HttpProxy to transmit data using the <b>jsonData</b> configuration-params of {@link Ext.Ajax#request}
  30. * instead of <b>params</b>.</p>
  31. * <p>When using a {@link Ext.data.Store#restful} Store, some serverside frameworks are
  32. * tuned to expect data through the jsonData mechanism. In those cases, one will want to set <b>encode: <tt>false</tt></b>, as in
  33. * let the lower-level connection object (eg: Ext.Ajax) do the encoding.</p>
  34. */
  35. encode : true,
  36. /**
  37. * @cfg {Boolean} encodeDelete False to send only the id to the server on delete, true to encode it in an object
  38. * literal, eg: <pre><code>
  39. {id: 1}
  40. * </code></pre> Defaults to <tt>false</tt>
  41. */
  42. encodeDelete: false,
  43. constructor : function(config){
  44. Ext.data.JsonWriter.superclass.constructor.call(this, config);
  45. },
  46. /**
  47. * <p>This method should not need to be called by application code, however it may be useful on occasion to
  48. * override it, or augment it with an {@link Function#createInterceptor interceptor} or {@link Function#createSequence sequence}.</p>
  49. * <p>The provided implementation encodes the serialized data representing the Store's modified Records into the Ajax request's
  50. * <code>params</code> according to the <code>{@link #encode}</code> setting.</p>
  51. * @param {Object} Ajax request params object to write into.
  52. * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}. The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}.
  53. * @param {Object/Object[]} data Data object representing the serialized modified records from the Store. May be either a single object,
  54. * or an Array of objects - user implementations must handle both cases.
  55. */
  56. render : function(params, baseParams, data) {
  57. if (this.encode === true) {
  58. // Encode here now.
  59. Ext.apply(params, baseParams);
  60. params[this.meta.root] = Ext.encode(data);
  61. } else {
  62. // defer encoding for some other layer, probably in {@link Ext.Ajax#request}. Place everything into "jsonData" key.
  63. var jdata = Ext.apply({}, baseParams);
  64. jdata[this.meta.root] = data;
  65. params.jsonData = jdata;
  66. }
  67. },
  68. /**
  69. * Implements abstract Ext.data.DataWriter#createRecord
  70. * @protected
  71. * @param {Ext.data.Record} rec
  72. * @return {Object}
  73. */
  74. createRecord : function(rec) {
  75. return this.toHash(rec);
  76. },
  77. /**
  78. * Implements abstract Ext.data.DataWriter#updateRecord
  79. * @protected
  80. * @param {Ext.data.Record} rec
  81. * @return {Object}
  82. */
  83. updateRecord : function(rec) {
  84. return this.toHash(rec);
  85. },
  86. /**
  87. * Implements abstract Ext.data.DataWriter#destroyRecord
  88. * @protected
  89. * @param {Ext.data.Record} rec
  90. * @return {Object}
  91. */
  92. destroyRecord : function(rec){
  93. if(this.encodeDelete){
  94. var data = {};
  95. data[this.meta.idProperty] = rec.id;
  96. return data;
  97. }else{
  98. return rec.id;
  99. }
  100. }
  101. });/**
  102. * @class Ext.data.JsonReader
  103. * @extends Ext.data.DataReader
  104. * <p>Data reader class to create an Array of {@link Ext.data.Record} objects
  105. * from a JSON packet based on mappings in a provided {@link Ext.data.Record}
  106. * constructor.</p>
  107. * <p>Example code:</p>
  108. * <pre><code>
  109. var myReader = new Ext.data.JsonReader({
  110. // metadata configuration options:
  111. {@link #idProperty}: 'id'
  112. {@link #root}: 'rows',
  113. {@link #totalProperty}: 'results',
  114. {@link Ext.data.DataReader#messageProperty}: "msg" // The element within the response that provides a user-feedback message (optional)
  115. // the fields config option will internally create an {@link Ext.data.Record}
  116. // constructor that provides mapping for reading the record data objects
  117. {@link Ext.data.DataReader#fields fields}: [
  118. // map Record&#39;s 'firstname' field to data object&#39;s key of same name
  119. {name: 'name', mapping: 'firstname'},
  120. // map Record&#39;s 'job' field to data object&#39;s 'occupation' key
  121. {name: 'job', mapping: 'occupation'}
  122. ]
  123. });
  124. </code></pre>
  125. * <p>This would consume a JSON data object of the form:</p><pre><code>
  126. {
  127. results: 2000, // Reader&#39;s configured {@link #totalProperty}
  128. rows: [ // Reader&#39;s configured {@link #root}
  129. // record data objects:
  130. { {@link #idProperty id}: 1, firstname: 'Bill', occupation: 'Gardener' },
  131. { {@link #idProperty id}: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
  132. ...
  133. ]
  134. }
  135. </code></pre>
  136. * <p><b><u>Automatic configuration using metaData</u></b></p>
  137. * <p>It is possible to change a JsonReader's metadata at any time by including
  138. * a <b><tt>metaData</tt></b> property in the JSON data object. If the JSON data
  139. * object has a <b><tt>metaData</tt></b> property, a {@link Ext.data.Store Store}
  140. * object using this Reader will reconfigure itself to use the newly provided
  141. * field definition and fire its {@link Ext.data.Store#metachange metachange}
  142. * event. The metachange event handler may interrogate the <b><tt>metaData</tt></b>
  143. * property to perform any configuration required.</p>
  144. * <p>Note that reconfiguring a Store potentially invalidates objects which may
  145. * refer to Fields or Records which no longer exist.</p>
  146. * <p>To use this facility you would create the JsonReader like this:</p><pre><code>
  147. var myReader = new Ext.data.JsonReader();
  148. </code></pre>
  149. * <p>The first data packet from the server would configure the reader by
  150. * containing a <b><tt>metaData</tt></b> property <b>and</b> the data. For
  151. * example, the JSON data object might take the form:</p><pre><code>
  152. {
  153. metaData: {
  154. "{@link #idProperty}": "id",
  155. "{@link #root}": "rows",
  156. "{@link #totalProperty}": "results"
  157. "{@link #successProperty}": "success",
  158. "{@link Ext.data.DataReader#fields fields}": [
  159. {"name": "name"},
  160. {"name": "job", "mapping": "occupation"}
  161. ],
  162. // used by store to set its sortInfo
  163. "sortInfo":{
  164. "field": "name",
  165. "direction": "ASC"
  166. },
  167. // {@link Ext.PagingToolbar paging data} (if applicable)
  168. "start": 0,
  169. "limit": 2,
  170. // custom property
  171. "foo": "bar"
  172. },
  173. // Reader&#39;s configured {@link #successProperty}
  174. "success": true,
  175. // Reader&#39;s configured {@link #totalProperty}
  176. "results": 2000,
  177. // Reader&#39;s configured {@link #root}
  178. // (this data simulates 2 results {@link Ext.PagingToolbar per page})
  179. "rows": [ // <b>*Note:</b> this must be an Array
  180. { "id": 1, "name": "Bill", "occupation": "Gardener" },
  181. { "id": 2, "name": "Ben", "occupation": "Horticulturalist" }
  182. ]
  183. }
  184. * </code></pre>
  185. * <p>The <b><tt>metaData</tt></b> property in the JSON data object should contain:</p>
  186. * <div class="mdetail-params"><ul>
  187. * <li>any of the configuration options for this class</li>
  188. * <li>a <b><tt>{@link Ext.data.Record#fields fields}</tt></b> property which
  189. * the JsonReader will use as an argument to the
  190. * {@link Ext.data.Record#create data Record create method} in order to
  191. * configure the layout of the Records it will produce.</li>
  192. * <li>a <b><tt>{@link Ext.data.Store#sortInfo sortInfo}</tt></b> property
  193. * which the JsonReader will use to set the {@link Ext.data.Store}'s
  194. * {@link Ext.data.Store#sortInfo sortInfo} property</li>
  195. * <li>any custom properties needed</li>
  196. * </ul></div>
  197. *
  198. * @constructor
  199. * Create a new JsonReader
  200. * @param {Object} meta Metadata configuration options.
  201. * @param {Array/Object} recordType
  202. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  203. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  204. * constructor created from {@link Ext.data.Record#create}.</p>
  205. */
  206. Ext.data.JsonReader = function(meta, recordType){
  207. meta = meta || {};
  208. /**
  209. * @cfg {String} idProperty [id] Name of the property within a row object
  210. * that contains a record identifier value. Defaults to <tt>id</tt>
  211. */
  212. /**
  213. * @cfg {String} successProperty [success] Name of the property from which to
  214. * retrieve the success attribute. Defaults to <tt>success</tt>. See
  215. * {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception}
  216. * for additional information.
  217. */
  218. /**
  219. * @cfg {String} totalProperty [total] Name of the property from which to
  220. * retrieve the total number of records in the dataset. This is only needed
  221. * if the whole dataset is not passed in one go, but is being paged from
  222. * the remote server. Defaults to <tt>total</tt>.
  223. */
  224. /**
  225. * @cfg {String} root [undefined] <b>Required</b>. The name of the property
  226. * which contains the Array of row objects. Defaults to <tt>undefined</tt>.
  227. * An exception will be thrown if the root property is undefined. The data
  228. * packet value for this property should be an empty array to clear the data
  229. * or show no data.
  230. */
  231. Ext.applyIf(meta, {
  232. idProperty: 'id',
  233. successProperty: 'success',
  234. totalProperty: 'total'
  235. });
  236. Ext.data.JsonReader.superclass.constructor.call(this, meta, recordType || meta.fields);
  237. };
  238. Ext.extend(Ext.data.JsonReader, Ext.data.DataReader, {
  239. /**
  240. * This JsonReader's metadata as passed to the constructor, or as passed in
  241. * the last data packet's <b><tt>metaData</tt></b> property.
  242. * @type Mixed
  243. * @property meta
  244. */
  245. /**
  246. * This method is only used by a DataProxy which has retrieved data from a remote server.
  247. * @param {Object} response The XHR object which contains the JSON data in its responseText.
  248. * @return {Object} data A data block which is used by an Ext.data.Store object as
  249. * a cache of Ext.data.Records.
  250. */
  251. read : function(response){
  252. var json = response.responseText;
  253. var o = Ext.decode(json);
  254. if(!o) {
  255. throw {message: 'JsonReader.read: Json object not found'};
  256. }
  257. return this.readRecords(o);
  258. },
  259. /*
  260. * TODO: refactor code between JsonReader#readRecords, #readResponse into 1 method.
  261. * there's ugly duplication going on due to maintaining backwards compat. with 2.0. It's time to do this.
  262. */
  263. /**
  264. * Decode a JSON response from server.
  265. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  266. * @param {Object} response The XHR object returned through an Ajax server request.
  267. * @return {Response} A {@link Ext.data.Response Response} object containing the data response, and also status information.
  268. */
  269. readResponse : function(action, response) {
  270. var o = (response.responseText !== undefined) ? Ext.decode(response.responseText) : response;
  271. if(!o) {
  272. throw new Ext.data.JsonReader.Error('response');
  273. }
  274. var root = this.getRoot(o),
  275. success = this.getSuccess(o);
  276. if (success && action === Ext.data.Api.actions.create) {
  277. var def = Ext.isDefined(root);
  278. if (def && Ext.isEmpty(root)) {
  279. throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
  280. }
  281. else if (!def) {
  282. throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
  283. }
  284. }
  285. // instantiate response object
  286. var res = new Ext.data.Response({
  287. action: action,
  288. success: success,
  289. data: (root) ? this.extractData(root, false) : [],
  290. message: this.getMessage(o),
  291. raw: o
  292. });
  293. // blow up if no successProperty
  294. if (Ext.isEmpty(res.success)) {
  295. throw new Ext.data.JsonReader.Error('successProperty-response', this.meta.successProperty);
  296. }
  297. return res;
  298. },
  299. /**
  300. * Create a data block containing Ext.data.Records from a JSON object.
  301. * @param {Object} o An object which contains an Array of row objects in the property specified
  302. * in the config as 'root, and optionally a property, specified in the config as 'totalProperty'
  303. * which contains the total size of the dataset.
  304. * @return {Object} data A data block which is used by an Ext.data.Store object as
  305. * a cache of Ext.data.Records.
  306. */
  307. readRecords : function(o){
  308. /**
  309. * After any data loads, the raw JSON data is available for further custom processing. If no data is
  310. * loaded or there is a load exception this property will be undefined.
  311. * @type Object
  312. */
  313. this.jsonData = o;
  314. if(o.metaData){
  315. this.onMetaChange(o.metaData);
  316. }
  317. var s = this.meta, Record = this.recordType,
  318. f = Record.prototype.fields, fi = f.items, fl = f.length, v;
  319. var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
  320. if(s.totalProperty){
  321. v = parseInt(this.getTotal(o), 10);
  322. if(!isNaN(v)){
  323. totalRecords = v;
  324. }
  325. }
  326. if(s.successProperty){
  327. v = this.getSuccess(o);
  328. if(v === false || v === 'false'){
  329. success = false;
  330. }
  331. }
  332. // TODO return Ext.data.Response instance instead. @see #readResponse
  333. return {
  334. success : success,
  335. records : this.extractData(root, true), // <-- true to return [Ext.data.Record]
  336. totalRecords : totalRecords
  337. };
  338. },
  339. // private
  340. buildExtractors : function() {
  341. if(this.ef){
  342. return;
  343. }
  344. var s = this.meta, Record = this.recordType,
  345. f = Record.prototype.fields, fi = f.items, fl = f.length;
  346. if(s.totalProperty) {
  347. this.getTotal = this.createAccessor(s.totalProperty);
  348. }
  349. if(s.successProperty) {
  350. this.getSuccess = this.createAccessor(s.successProperty);
  351. }
  352. if (s.messageProperty) {
  353. this.getMessage = this.createAccessor(s.messageProperty);
  354. }
  355. this.getRoot = s.root ? this.createAccessor(s.root) : function(p){return p;};
  356. if (s.id || s.idProperty) {
  357. var g = this.createAccessor(s.id || s.idProperty);
  358. this.getId = function(rec) {
  359. var r = g(rec);
  360. return (r === undefined || r === '') ? null : r;
  361. };
  362. } else {
  363. this.getId = function(){return null;};
  364. }
  365. var ef = [];
  366. for(var i = 0; i < fl; i++){
  367. f = fi[i];
  368. var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
  369. ef.push(this.createAccessor(map));
  370. }
  371. this.ef = ef;
  372. },
  373. /**
  374. * @ignore
  375. * TODO This isn't used anywhere?? Don't we want to use this where possible instead of complex #createAccessor?
  376. */
  377. simpleAccess : function(obj, subsc) {
  378. return obj[subsc];
  379. },
  380. /**
  381. * @ignore
  382. */
  383. createAccessor : function(){
  384. var re = /[\[\.]/;
  385. return function(expr) {
  386. if(Ext.isEmpty(expr)){
  387. return Ext.emptyFn;
  388. }
  389. if(Ext.isFunction(expr)){
  390. return expr;
  391. }
  392. var i = String(expr).search(re);
  393. if(i >= 0){
  394. return new Function('obj', 'return obj' + (i > 0 ? '.' : '') + expr);
  395. }
  396. return function(obj){
  397. return obj[expr];
  398. };
  399. };
  400. }(),
  401. /**
  402. * type-casts a single row of raw-data from server
  403. * @param {Object} data
  404. * @param {Array} items
  405. * @param {Integer} len
  406. * @private
  407. */
  408. extractValues : function(data, items, len) {
  409. var f, values = {};
  410. for(var j = 0; j < len; j++){
  411. f = items[j];
  412. var v = this.ef[j](data);
  413. values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
  414. }
  415. return values;
  416. }
  417. });
  418. /**
  419. * @class Ext.data.JsonReader.Error
  420. * Error class for JsonReader
  421. */
  422. Ext.data.JsonReader.Error = Ext.extend(Ext.Error, {
  423. constructor : function(message, arg) {
  424. this.arg = arg;
  425. Ext.Error.call(this, message);
  426. },
  427. name : 'Ext.data.JsonReader'
  428. });
  429. Ext.apply(Ext.data.JsonReader.Error.prototype, {
  430. lang: {
  431. 'response': 'An error occurred while json-decoding your server response',
  432. 'successProperty-response': 'Could not locate your "successProperty" in your server response. Please review your JsonReader config to ensure the config-property "successProperty" matches the property in your server-response. See the JsonReader docs.',
  433. 'root-undefined-config': 'Your JsonReader was configured without a "root" property. Please review your JsonReader config and make sure to define the root property. See the JsonReader docs.',
  434. 'idProperty-undefined' : 'Your JsonReader was configured without an "idProperty" Please review your JsonReader configuration and ensure the "idProperty" is set (e.g.: "id"). See the JsonReader docs.',
  435. 'root-empty': 'Data was expected to be returned by the server in the "root" property of the response. Please review your JsonReader configuration to ensure the "root" property matches that returned in the server-response. See JsonReader docs.'
  436. }
  437. });
  438. /**
  439. * @class Ext.data.ArrayReader
  440. * @extends Ext.data.JsonReader
  441. * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an Array.
  442. * Each element of that Array represents a row of data fields. The
  443. * fields are pulled into a Record object using as a subscript, the <code>mapping</code> property
  444. * of the field definition if it exists, or the field's ordinal position in the definition.</p>
  445. * <p>Example code:</p>
  446. * <pre><code>
  447. var Employee = Ext.data.Record.create([
  448. {name: 'name', mapping: 1}, // "mapping" only needed if an "id" field is present which
  449. {name: 'occupation', mapping: 2} // precludes using the ordinal position as the index.
  450. ]);
  451. var myReader = new Ext.data.ArrayReader({
  452. {@link #idIndex}: 0
  453. }, Employee);
  454. </code></pre>
  455. * <p>This would consume an Array like this:</p>
  456. * <pre><code>
  457. [ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
  458. * </code></pre>
  459. * @constructor
  460. * Create a new ArrayReader
  461. * @param {Object} meta Metadata configuration options.
  462. * @param {Array/Object} recordType
  463. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  464. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  465. * constructor created from {@link Ext.data.Record#create}.</p>
  466. */
  467. Ext.data.ArrayReader = Ext.extend(Ext.data.JsonReader, {
  468. /**
  469. * @cfg {String} successProperty
  470. * @hide
  471. */
  472. /**
  473. * @cfg {Number} id (optional) The subscript within row Array that provides an ID for the Record.
  474. * Deprecated. Use {@link #idIndex} instead.
  475. */
  476. /**
  477. * @cfg {Number} idIndex (optional) The subscript within row Array that provides an ID for the Record.
  478. */
  479. /**
  480. * Create a data block containing Ext.data.Records from an Array.
  481. * @param {Object} o An Array of row objects which represents the dataset.
  482. * @return {Object} data A data block which is used by an Ext.data.Store object as
  483. * a cache of Ext.data.Records.
  484. */
  485. readRecords : function(o){
  486. this.arrayData = o;
  487. var s = this.meta,
  488. sid = s ? Ext.num(s.idIndex, s.id) : null,
  489. recordType = this.recordType,
  490. fields = recordType.prototype.fields,
  491. records = [],
  492. success = true,
  493. v;
  494. var root = this.getRoot(o);
  495. for(var i = 0, len = root.length; i < len; i++) {
  496. var n = root[i],
  497. values = {},
  498. id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
  499. for(var j = 0, jlen = fields.length; j < jlen; j++) {
  500. var f = fields.items[j],
  501. k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
  502. v = n[k] !== undefined ? n[k] : f.defaultValue;
  503. v = f.convert(v, n);
  504. values[f.name] = v;
  505. }
  506. var record = new recordType(values, id);
  507. record.json = n;
  508. records[records.length] = record;
  509. }
  510. var totalRecords = records.length;
  511. if(s.totalProperty) {
  512. v = parseInt(this.getTotal(o), 10);
  513. if(!isNaN(v)) {
  514. totalRecords = v;
  515. }
  516. }
  517. if(s.successProperty){
  518. v = this.getSuccess(o);
  519. if(v === false || v === 'false'){
  520. success = false;
  521. }
  522. }
  523. return {
  524. success : success,
  525. records : records,
  526. totalRecords : totalRecords
  527. };
  528. }
  529. });/**
  530. * @class Ext.data.ArrayStore
  531. * @extends Ext.data.Store
  532. * <p>Formerly known as "SimpleStore".</p>
  533. * <p>Small helper class to make creating {@link Ext.data.Store}s from Array data easier.
  534. * An ArrayStore will be automatically configured with a {@link Ext.data.ArrayReader}.</p>
  535. * <p>A store configuration would be something like:<pre><code>
  536. var store = new Ext.data.ArrayStore({
  537. // store configs
  538. autoDestroy: true,
  539. storeId: 'myStore',
  540. // reader configs
  541. idIndex: 0,
  542. fields: [
  543. 'company',
  544. {name: 'price', type: 'float'},
  545. {name: 'change', type: 'float'},
  546. {name: 'pctChange', type: 'float'},
  547. {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
  548. ]
  549. });
  550. * </code></pre></p>
  551. * <p>This store is configured to consume a returned object of the form:<pre><code>
  552. var myData = [
  553. ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
  554. ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
  555. ['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'],
  556. ['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],
  557. ['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am']
  558. ];
  559. * </code></pre>
  560. * An object literal of this form could also be used as the {@link #data} config option.</p>
  561. * <p><b>*Note:</b> Although not listed here, this class accepts all of the configuration options of
  562. * <b>{@link Ext.data.ArrayReader ArrayReader}</b>.</p>
  563. * @constructor
  564. * @param {Object} config
  565. * @xtype arraystore
  566. */
  567. Ext.data.ArrayStore = Ext.extend(Ext.data.Store, {
  568. /**
  569. * @cfg {Ext.data.DataReader} reader @hide
  570. */
  571. constructor: function(config){
  572. Ext.data.ArrayStore.superclass.constructor.call(this, Ext.apply(config, {
  573. reader: new Ext.data.ArrayReader(config)
  574. }));
  575. },
  576. loadData : function(data, append){
  577. if(this.expandData === true){
  578. var r = [];
  579. for(var i = 0, len = data.length; i < len; i++){
  580. r[r.length] = [data[i]];
  581. }
  582. data = r;
  583. }
  584. Ext.data.ArrayStore.superclass.loadData.call(this, data, append);
  585. }
  586. });
  587. Ext.reg('arraystore', Ext.data.ArrayStore);
  588. // backwards compat
  589. Ext.data.SimpleStore = Ext.data.ArrayStore;
  590. Ext.reg('simplestore', Ext.data.SimpleStore);/**
  591. * @class Ext.data.JsonStore
  592. * @extends Ext.data.Store
  593. * <p>Small helper class to make creating {@link Ext.data.Store}s from JSON data easier.
  594. * A JsonStore will be automatically configured with a {@link Ext.data.JsonReader}.</p>
  595. * <p>A store configuration would be something like:<pre><code>
  596. var store = new Ext.data.JsonStore({
  597. // store configs
  598. autoDestroy: true,
  599. url: 'get-images.php',
  600. storeId: 'myStore',
  601. // reader configs
  602. root: 'images',
  603. idProperty: 'name',
  604. fields: ['name', 'url', {name:'size', type: 'float'}, {name:'lastmod', type:'date'}]
  605. });
  606. * </code></pre></p>
  607. * <p>This store is configured to consume a returned object of the form:<pre><code>
  608. {
  609. images: [
  610. {name: 'Image one', url:'/GetImage.php?id=1', size:46.5, lastmod: new Date(2007, 10, 29)},
  611. {name: 'Image Two', url:'/GetImage.php?id=2', size:43.2, lastmod: new Date(2007, 10, 30)}
  612. ]
  613. }
  614. * </code></pre>
  615. * An object literal of this form could also be used as the {@link #data} config option.</p>
  616. * <p><b>*Note:</b> Although not listed here, this class accepts all of the configuration options of
  617. * <b>{@link Ext.data.JsonReader JsonReader}</b>.</p>
  618. * @constructor
  619. * @param {Object} config
  620. * @xtype jsonstore
  621. */
  622. Ext.data.JsonStore = Ext.extend(Ext.data.Store, {
  623. /**
  624. * @cfg {Ext.data.DataReader} reader @hide
  625. */
  626. constructor: function(config){
  627. Ext.data.JsonStore.superclass.constructor.call(this, Ext.apply(config, {
  628. reader: new Ext.data.JsonReader(config)
  629. }));
  630. }
  631. });
  632. Ext.reg('jsonstore', Ext.data.JsonStore);