71317518ae0682150b5b9610d75c130879bf5725.svn-base 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.data.ArrayReader
  8. * @extends Ext.data.DataReader Data reader class to create an Array of
  9. * Ext.data.Record objects from an Array. Each element of that Array
  10. * represents a row of data fields. The fields are pulled into a Record
  11. * object using as a subscript, the <em>mapping</em> property of the
  12. * field definition if it exists, or the field's ordinal position in
  13. * the definition.<br>
  14. * <p>
  15. * Example code:.
  16. *
  17. * <pre><code>
  18. * var Employee = Ext.data.Record.create([{
  19. * name : 'name',
  20. * mapping : 1
  21. * }, // &quot;mapping&quot; only needed if an &quot;id&quot; field is present which
  22. * {
  23. * name : 'occupation',
  24. * mapping : 2
  25. * } // precludes using the ordinal position as the index.
  26. * ]);
  27. * var myReader = new Ext.data.ArrayReader({
  28. * id : 0
  29. * // The subscript within row Array that provides an ID for the Record (optional)
  30. * }, Employee);
  31. * </code></pre>
  32. *
  33. * <p>
  34. * This would consume an Array like this:
  35. *
  36. * <pre><code>
  37. * [[1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist']]
  38. * </code></pre>
  39. *
  40. * @cfg {String} id (optional) The subscript within row Array that provides an
  41. * ID for the Record
  42. * @constructor Create a new ArrayReader
  43. * @param {Object}
  44. * meta Metadata configuration options.
  45. * @param {Object}
  46. * recordType Either an Array of field definition objects as
  47. * specified to {@link Ext.data.Record#create}, or a
  48. * {@link Ext.data.Record Record} constructor created using
  49. * {@link Ext.data.Record#create}.
  50. */
  51. Ext.data.ArrayReader = Ext.extend(Ext.data.JsonReader, {
  52. /**
  53. * Create a data block containing Ext.data.Records from an XML
  54. * document.
  55. *
  56. * @param {Object}
  57. * o An Array of row objects which represents the
  58. * dataset.
  59. * @return {Object} data A data block which is used by an
  60. * Ext.data.Store object as a cache of Ext.data.Records.
  61. */
  62. readRecords : function(o) {
  63. var sid = this.meta ? this.meta.id : null;
  64. var recordType = this.recordType, fields = recordType.prototype.fields;
  65. var records = [];
  66. var root = o;
  67. for (var i = 0; i < root.length; i++) {
  68. var n = root[i];
  69. var values = {};
  70. var id = ((sid || sid === 0) && n[sid] !== undefined
  71. && n[sid] !== "" ? n[sid] : null);
  72. for (var j = 0, jlen = fields.length; j < jlen; j++) {
  73. var f = fields.items[j];
  74. var k = f.mapping !== undefined && f.mapping !== null
  75. ? f.mapping
  76. : j;
  77. var v = n[k] !== undefined ? n[k] : f.defaultValue;
  78. v = f.convert(v);
  79. values[f.name] = v;
  80. }
  81. var record = new recordType(values, id);
  82. record.json = n;
  83. records[records.length] = record;
  84. }
  85. return {
  86. records : records,
  87. totalRecords : records.length
  88. };
  89. }
  90. });