RpcService.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. define([
  2. "../_base/array", "../_base/declare", "../_base/Deferred", "../_base/kernel","../_base/lang",
  3. "../_base/url", "../_base/xhr"
  4. ], function(array, declare, Deferred, kernel, lang, _Url, xhr){
  5. // module:
  6. // dojo/rpc/RpcService
  7. return declare("dojo.rpc.RpcService", null, {
  8. // summary:
  9. // TODOC
  10. constructor: function(args){
  11. // summary:
  12. // Take a string as a url to retrieve an smd or an object that is an smd or partial smd to use
  13. // as a definition for the service
  14. //
  15. // args: object
  16. // Takes a number of properties as kwArgs for defining the service. It also
  17. // accepts a string. When passed a string, it is treated as a url from
  18. // which it should synchronously retrieve an smd file. Otherwise it is a kwArgs
  19. // object. It accepts serviceUrl, to manually define a url for the rpc service
  20. // allowing the rpc system to be used without an smd definition. strictArgChecks
  21. // forces the system to verify that the # of arguments provided in a call
  22. // matches those defined in the smd. smdString allows a developer to pass
  23. // a jsonString directly, which will be converted into an object or alternatively
  24. // smdObject is accepts an smdObject directly.
  25. //
  26. if(args){
  27. //if the arg is a string, we assume it is a url to retrieve an smd definition from
  28. if( (lang.isString(args)) || (args instanceof _Url)){
  29. if (args instanceof _Url){
  30. var url = args + "";
  31. }else{
  32. url = args;
  33. }
  34. var def = xhr.get({
  35. url: url,
  36. handleAs: "json-comment-optional",
  37. sync: true
  38. });
  39. def.addCallback(this, "processSmd");
  40. def.addErrback(function(){
  41. throw new Error("Unable to load SMD from " + args);
  42. });
  43. }else if(args.smdStr){
  44. this.processSmd(kernel.eval("("+args.smdStr+")"));
  45. }else{
  46. // otherwise we assume it's an arguments object with the following
  47. // (optional) properties:
  48. // - serviceUrl
  49. // - strictArgChecks
  50. // - smdStr
  51. // - smdObj
  52. if(args.serviceUrl){
  53. this.serviceUrl = args.serviceUrl;
  54. }
  55. this.timeout = args.timeout || 0;
  56. if("strictArgChecks" in args){
  57. this.strictArgChecks = args.strictArgChecks;
  58. }
  59. this.processSmd(args);
  60. }
  61. }
  62. },
  63. strictArgChecks: true,
  64. serviceUrl: "",
  65. parseResults: function(obj){
  66. // summary:
  67. // parse the results coming back from an rpc request. this
  68. // base implementation, just returns the full object
  69. // subclasses should parse and only return the actual results
  70. // obj: Object
  71. // Object that is the return results from an rpc request
  72. return obj;
  73. },
  74. errorCallback: function(/* dojo/_base/Deferred */ deferredRequestHandler){
  75. // summary:
  76. // create callback that calls the Deferred errback method
  77. // deferredRequestHandler: Deferred
  78. // The deferred object handling a request.
  79. return function(data){
  80. deferredRequestHandler.errback(data.message);
  81. };
  82. },
  83. resultCallback: function(/* dojo/_base/Deferred */ deferredRequestHandler){
  84. // summary:
  85. // create callback that calls the Deferred's callback method
  86. // deferredRequestHandler: Deferred
  87. // The deferred object handling a request.
  88. return lang.hitch(this,
  89. function(obj){
  90. if(obj.error!=null){
  91. var err;
  92. if(typeof obj.error == 'object'){
  93. err = new Error(obj.error.message);
  94. err.code = obj.error.code;
  95. err.error = obj.error.error;
  96. }else{
  97. err = new Error(obj.error);
  98. }
  99. err.id = obj.id;
  100. err.errorObject = obj;
  101. deferredRequestHandler.errback(err);
  102. }else{
  103. deferredRequestHandler.callback(this.parseResults(obj));
  104. }
  105. }
  106. );
  107. },
  108. generateMethod: function(/*string*/ method, /*array*/ parameters, /*string*/ url){
  109. // summary:
  110. // generate the local bind methods for the remote object
  111. // method: string
  112. // The name of the method we are generating
  113. // parameters: array
  114. // the array of parameters for this call.
  115. // url: string
  116. // the service url for this call
  117. return lang.hitch(this, function(){
  118. var deferredRequestHandler = new Deferred();
  119. // if params weren't specified, then we can assume it's varargs
  120. if( (this.strictArgChecks) &&
  121. (parameters != null) &&
  122. (arguments.length != parameters.length)
  123. ){
  124. // put error stuff here, no enough params
  125. throw new Error("Invalid number of parameters for remote method.");
  126. }else{
  127. this.bind(method, lang._toArray(arguments), deferredRequestHandler, url);
  128. }
  129. return deferredRequestHandler;
  130. });
  131. },
  132. processSmd: function(object){
  133. // summary:
  134. // callback method for receipt of a smd object. Parse the smd
  135. // and generate functions based on the description
  136. // object:
  137. // smd object defining this service.
  138. if(object.methods){
  139. array.forEach(object.methods, function(m){
  140. if(m && m.name){
  141. this[m.name] = this.generateMethod( m.name,
  142. m.parameters,
  143. m.url||m.serviceUrl||m.serviceURL);
  144. if(!lang.isFunction(this[m.name])){
  145. throw new Error("RpcService: Failed to create" + m.name + "()");
  146. /*console.log("RpcService: Failed to create", m.name, "()");*/
  147. }
  148. }
  149. }, this);
  150. }
  151. this.serviceUrl = object.serviceUrl||object.serviceURL;
  152. this.required = object.required;
  153. this.smd = object;
  154. }
  155. });
  156. });