JsonReader-min.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. Ext.data.JsonReader = function(A, B) {
  7. A = A || {};
  8. Ext.data.JsonReader.superclass.constructor.call(this, A, B || A.fields)
  9. };
  10. Ext.extend(Ext.data.JsonReader, Ext.data.DataReader, {
  11. read : function(response) {
  12. var json = response.responseText;
  13. var o = eval("(" + json + ")");
  14. if (!o) {
  15. throw {
  16. message : "JsonReader.read: Json object not found"
  17. }
  18. }
  19. if (o.metaData) {
  20. delete this.ef;
  21. this.meta = o.metaData;
  22. this.recordType = Ext.data.Record.create(o.metaData.fields);
  23. this.onMetaChange(this.meta, this.recordType, o)
  24. }
  25. return this.readRecords(o)
  26. },
  27. onMetaChange : function(A, C, B) {
  28. },
  29. simpleAccess : function(B, A) {
  30. return B[A]
  31. },
  32. getJsonAccessor : function() {
  33. var A = /[\[\.]/;
  34. return function(C) {
  35. try {
  36. return (A.test(C))
  37. ? new Function("obj", "return obj." + C)
  38. : function(D) {
  39. return D[C]
  40. }
  41. } catch (B) {
  42. }
  43. return Ext.emptyFn
  44. }
  45. }(),
  46. readRecords : function(K) {
  47. this.jsonData = K;
  48. var H = this.meta, A = this.recordType, R = A.prototype.fields, F = R.items, E = R.length;
  49. if (!this.ef) {
  50. if (H.totalProperty) {
  51. this.getTotal = this.getJsonAccessor(H.totalProperty)
  52. }
  53. if (H.successProperty) {
  54. this.getSuccess = this.getJsonAccessor(H.successProperty)
  55. }
  56. this.getRoot = H.root ? this.getJsonAccessor(H.root) : function(U) {
  57. return U
  58. };
  59. if (H.id) {
  60. var Q = this.getJsonAccessor(H.id);
  61. this.getId = function(V) {
  62. var U = Q(V);
  63. return (U === undefined || U === "") ? null : U
  64. }
  65. } else {
  66. this.getId = function() {
  67. return null
  68. }
  69. }
  70. this.ef = [];
  71. for (var O = 0; O < E; O++) {
  72. R = F[O];
  73. var T = (R.mapping !== undefined && R.mapping !== null)
  74. ? R.mapping
  75. : R.name;
  76. this.ef[O] = this.getJsonAccessor(T)
  77. }
  78. }
  79. var M = this.getRoot(K), S = M.length, I = S, D = true;
  80. if (H.totalProperty) {
  81. var G = parseInt(this.getTotal(K), 10);
  82. if (!isNaN(G)) {
  83. I = G
  84. }
  85. }
  86. if (H.successProperty) {
  87. var G = this.getSuccess(K);
  88. if (G === false || G === "false") {
  89. D = false
  90. }
  91. }
  92. var P = [];
  93. for (var O = 0; O < S; O++) {
  94. var L = M[O];
  95. var B = {};
  96. var J = this.getId(L);
  97. for (var N = 0; N < E; N++) {
  98. R = F[N];
  99. var G = this.ef[N](L);
  100. B[R.name] = R.convert((G !== undefined) ? G : R.defaultValue)
  101. }
  102. var C = new A(B, J);
  103. C.json = L;
  104. P[O] = C
  105. }
  106. return {
  107. success : D,
  108. records : P,
  109. totalRecords : I
  110. }
  111. }
  112. });