RpcService.js 5.0 KB

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