PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/netgestion/V4_04/Programme/Nouveau/CFQueryReader.js

http://parser.googlecode.com/
JavaScript | 203 lines | 91 code | 13 blank | 99 comment | 26 complexity | efa995ddb55c814e159c2f022371fdf6 MD5 | raw file
  1. /*
  2. * Ext JS Library 2.0
  3. * Copyright(c) 2006-2007, Ext JS, LLC.
  4. * licensing@extjs.com
  5. *
  6. * http://extjs.com/license
  7. *
  8. *******************************************
  9. * Steve 'Cutter' Blades (CutterBl) no.junkATcutterscrossingDOTcom
  10. * http://blog.cutterscrossing.com
  11. *
  12. * @@@ Ext.data.CFQueryReader v 1.1 [05.12.09] @@@
  13. *
  14. * Inspired by the CFJsonReader, originally writtin by John Wilson (Daemach)
  15. * http://extjs.com/forum/showthread.php?t=21408&highlight=cfjsonreader
  16. *
  17. * This Custom Data Reader will take the JSON return of a ColdFusion
  18. * Query object, rather returned straight up, or via the ColdFusion
  19. * QueryConvertForGrid() method, or even as a key within a struct.
  20. *
  21. * The CFQueryReader constructor takes two arguments
  22. * @meta : object containing key/value pairs for each record (See ArrayReader Config Options)
  23. * @recordType : field mapping object
  24. *
  25. * ------------------------------------------------------------------------------------
  26. * REVISON: [05.12.09]
  27. * Tested, and fully functional, with Ext JS 2.2, and a fresh (20:30 CDT) build of Ext 3.0
  28. * ------------------------------------------------------------------------------------
  29. * REVISON: [03.10.09]
  30. * Through the 'meta' argument, you can now define which JSON node is the 'root',
  31. * 'success', or the 'totalProperty', as well as the 'id'.
  32. *
  33. * If 'root' is undefined in the meta then it will look for a 'QUERY' node in the
  34. * object root, otherwise it will assume the object root is the root of the JSON object.
  35. *
  36. * if the object root contains a 'TOTALROWCOUNT' node, it will apply it's value
  37. * to the totalRowCount. If a 'totalProperty' is defined in the meta, it will
  38. * take the node of that name value, and apply it's value to the totalRowCount. If
  39. * neither is available, it will automatically count all records processed, and
  40. * apply that count to the value of totalRowCount.
  41. *
  42. * With this revision, we also remove the requirement for using uppercase values for
  43. * 'mapping' definitions
  44. * -------------------------------------------------------------------------------------
  45. *
  46. * The recordType object allows you to alias the returned ColdFusion column
  47. * name (which is always passed in upper case) to any 'name' you wish, as
  48. * well as assign a data type, which your ExtJS app will attempt to cast
  49. * whenever the value is referenced.
  50. *
  51. * ColdFusion's JSON return, for a ColdFusion Query object, will appear in the
  52. * following format:
  53. *
  54. * {"COLUMNS":["INTVENDORTYPEID","STRVENDORTYPE","INTEXPENSECATEGORIESID",
  55. * "STREXPENSECATEGORIES"],"DATA" :[[2,"Carpet Cleaning",1,"Cleaining"],
  56. * [1,"Cleaning Service",1,"Cleaining"]]}
  57. *
  58. * The ColdFusion JSON return on any query that is first passed through
  59. * ColdFusion's QueryConvertForGrid() method will return the object in the
  60. * following format:
  61. *
  62. * {"TOTALROWCOUNT":3, "QUERY":{"COLUMNS":["MYIDFIELD","DATA1","DATA2"],
  63. * "DATA":[[1,"Bob","Smith"],[6,"Jim","Brown"]]}}
  64. *
  65. * The Ext.data.CFQueryReader is designed to accomodate either format
  66. * automatically. You would create your reader instance in much the same
  67. * way as the CFJsonReader was created:
  68. *
  69. * var myDataModel = [
  70. * {name: 'myIdField', mapping: 'myIdField'},
  71. * {name: 'data1', mapping: 'data1'},
  72. * {name: 'data2', mapping: 'data2'}
  73. * ];
  74. *
  75. * var myCFReader = new Ext.data.CFQueryReader({id:'myIdField'},myDataModel);
  76. *
  77. * Notice that the 'id' meta value mirrors the alias 'name' of the record's field.
  78. *
  79. * You could also define your reader within the Store configuration:
  80. *
  81. * var myStore = new Ext.data.Store({
  82. * reader: new Ext.data.CFQueryReader({
  83. * id:'myIdField', // matches the 'name' attribute of your id field
  84. * root:'rootOfQueryJSON', // Case Specific
  85. * totalProperty:'someTotalsNode' // Case Specific
  86. * },[
  87. * {name: 'myIdField', mapping: 'myIdField'},
  88. * {name: 'data1', mapping: 'data1'},
  89. * {name: 'data2', mapping: 'data2'}
  90. * ]),
  91. * url:'/path/to/some/remote/proxy.cfc',
  92. * baseParams:{
  93. * method:'myMethod',
  94. * returnFormat:'JSON'
  95. * someMethodArg: numericValue
  96. * anotherMethodArg: 'stringValue'
  97. * }
  98. * });
  99. */
  100. Ext.data.CFQueryReader = Ext.extend(Ext.data.ArrayReader, {
  101. read : function(response){
  102. var json = response.responseText;
  103. var o = eval("("+json+")");
  104. if(!o) {
  105. throw {message: "CFQueryReader.read: Json object not found"};
  106. }
  107. return this.readRecords(o);
  108. },
  109. readRecords : function(o){
  110. this.jsonData = o;
  111. var s = this.meta, Record = this.recordType,
  112. f = Record.prototype.fields, fi = f.items, fl = f.length;
  113. if (!this.ef) {
  114. if(s.successProperty) {
  115. this.getSuccess = this.getJsonAccessor(s.successProperty);
  116. }
  117. if(s.root){
  118. this.getRoot = this.getJsonAccessor(s.root + '.DATA');
  119. this.getQueryRoot = this.getJsonAccessor(s.root);
  120. } else {
  121. this.getRoot = (o.QUERY) ? this.getJsonAccessor('QUERY.DATA') : this.getJsonAccessor('DATA');
  122. this.getQueryRoot = function(){
  123. return (o.QUERY) ? o.QUERY : o;
  124. };
  125. }
  126. if(s.totalProperty) {
  127. this.getTotal = this.getJsonAccessor(s.totalProperty);
  128. } else if(o.TOTALROWCOUNT) {
  129. this.getTotal = this.getJsonAccessor('TOTALROWCOUNT');
  130. }
  131. }
  132. var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
  133. var cols = this.getQueryRoot(o).COLUMNS;
  134. if(s.totalProperty || o.TOTALROWCOUNT){
  135. var v = parseInt(this.getTotal(o), 10);
  136. if(!isNaN(v)){
  137. totalRecords = v;
  138. }
  139. }
  140. if(s.successProperty){
  141. var v = this.getSuccess(o);
  142. if(v === false || v === 'false'){
  143. success = false;
  144. }
  145. }
  146. for(b=0;b < fl; b++){
  147. var fMap = (fi[b].mapping !== undefined && fi[b].mapping !== null) ? fi[b].mapping : fi[b].name;
  148. fi[b].mapArrLoc = cols.indexOf(Ext.util.Format.uppercase(fMap));
  149. }
  150. if (!this.ef) {
  151. if (s.id) {
  152. this.getId = function(rec){
  153. var r = rec[s.id];
  154. return (r === undefined || r === "") ? null : r;
  155. };
  156. } else {
  157. this.getId = function(){return null;};
  158. }
  159. this.ef = [];
  160. for(var i = 0; i < fl; i++){
  161. f = fi[i];
  162. var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
  163. this.ef[i] = function(rec){
  164. var r = rec[map];
  165. return (r === undefined || r === "") ? null : r;
  166. };
  167. }
  168. }
  169. var records = [];
  170. for(var i = 0; i < c; i++){
  171. var n = root[i];
  172. var values = {};
  173. for(var j = 0, jlen = fl; j < jlen; j++){
  174. f = fi[j];
  175. var k = f.mapArrLoc !== undefined && f.mapArrLoc !== null ? f.mapArrLoc : j;
  176. var v = n[k] !== undefined ? n[k] : f.defaultValue;
  177. v = f.convert(v, n);
  178. values[f.name] = v;
  179. }
  180. var rec = new this.recordType(values, this.getId(values));
  181. rec.json = values;
  182. records[i] = rec;
  183. }
  184. return {
  185. success: success,
  186. records : records,
  187. totalRecords : totalRecords
  188. };
  189. }
  190. });