rpc.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * (c) 2010 jxf - rpc module.
  3. *
  4. * The idea of behind this module is fetching data via an RPC-like call:
  5. * . RESTful web services
  6. * . XML/SOAP (not yet implemented)
  7. * . Remote document (HTML; XML; JSON)
  8. * . Local documents (HTML; XML; JSON)
  9. * The module will return the data to a callback function should a parser be specified, the module will parse the data and return the parsed data to the callback function
  10. * This allows the client code to minimize parsing should returned data be in a standard format.
  11. * TODO:
  12. * Improve on how returned data is handled (if necessary).
  13. */
  14. if(!jx){
  15. var jx = {}
  16. }
  17. /**
  18. * These are a few parsers that can come in handy:
  19. * urlparser: This parser is intended to break down a url parameter string in key,value pairs
  20. */
  21. function urlparser(url){
  22. if(url.toString().match(/\x3F/) != null){
  23. url = url.split('\x3F')[1]
  24. }
  25. var p = url.split('&') ;
  26. var r = {} ;
  27. r.meta = [] ;
  28. r.data = {} ;
  29. var entry;
  30. for(var i=0; i < p.length; i++){
  31. entry = p[i] ;
  32. key = (entry.match('(.*)=') !=null)? entry.match('(.*)=')[1]:null ;
  33. value = (entry.match('=(.*)$') != null)? entry.match('=(.*)$')[1]:null
  34. if(key != null){
  35. key = key.replace('\x3F','')
  36. r.meta.push(key) ;
  37. r.data[key] = value ;
  38. }
  39. }
  40. return r.data;
  41. }
  42. /**
  43. * The following are corrections related to consistency in style & cohesion
  44. */
  45. jx.ajax = {}
  46. jx.ajax.get = {} ;
  47. jx.ajax.debug = null;
  48. jx.ajax.get.instance = function(){
  49. var factory = function(){
  50. this.obj = {}
  51. this.obj.headers = {}
  52. this.obj.async = true;
  53. this.setHeader = function(key,value){
  54. if(key.constructor != String && value == null){
  55. this.obj.headers = key ;
  56. }else{
  57. this.obj.headers[key] = value;
  58. }
  59. }
  60. this.setData = function(data,mimetype){
  61. if(mimetype == null)
  62. this.obj.data = data;
  63. else {
  64. this.obj.headers['Content-Type'] = mimetype
  65. if(mimetype.match(/application\/json/i)){
  66. this.obj.data = JSON.stringify(data)
  67. }
  68. }
  69. }
  70. this.setAsync = function(flag){
  71. this.obj.async = (flag == true) ;
  72. }
  73. this.send = function(url,callback,method){
  74. if(method == null){
  75. method = 'GET'
  76. }
  77. p = jx.ajax.debug != null;
  78. q = false;
  79. if(p){
  80. q = jx.ajax.debug[url] != null;
  81. }
  82. is_debuggable = p && q
  83. if(is_debuggable){
  84. x = {} ;
  85. x.responseText = jx.ajax.debug [url] ;
  86. callback(x)
  87. }else{
  88. var http = new XMLHttpRequest() ;
  89. http.onreadystatechange = function(){
  90. if(http.readyState == 4){
  91. callback(http)
  92. }
  93. }
  94. //
  95. // In order to insure backward compatibility
  96. // Previous versions allowed the user to set the variable on the wrapper (poor design)
  97. if(this.async != null){
  98. this.setAsync(this.async) ;
  99. }
  100. http.open(method,url,this.obj.async) ;
  101. for(key in this.obj.headers){
  102. value = this.obj.headers[key] ;
  103. http.setRequestHeader(key,value)
  104. }
  105. http.send(this.obj.data)
  106. }
  107. }
  108. this.put = function(url,callback){
  109. this.send(url,callback,'PUT') ;
  110. }
  111. this.get = function(url,callback){
  112. this.send(url,callback,'GET') ;
  113. }
  114. this.post = function(url,callback){
  115. this.send(url,callback,'POST') ;
  116. }
  117. }//-- end of the factory method
  118. return new factory() ;
  119. }
  120. //
  121. // backward compatibility
  122. jx.ajax.getInstance = jx.ajax.get.instance ;
  123. var HttpClient = jx.ajax.get ;