RpcService.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.rpc.RpcService"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.rpc.RpcService"] = true;
  8. dojo.provide("dojo.rpc.RpcService");
  9. dojo.declare("dojo.rpc.RpcService", null, {
  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( (dojo.isString(args)) || (args instanceof dojo._Url)){
  29. if (args instanceof dojo._Url){
  30. var url = args + "";
  31. }else{
  32. url = args;
  33. }
  34. var def = dojo.xhrGet({
  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(dojo.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 || 3000;
  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.Deferred */ deferredRequestHandler){
  75. // summary:
  76. // create callback that calls the Deferres 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.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. var tf = dojo.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. return tf;
  108. },
  109. generateMethod: function(/*string*/ method, /*array*/ parameters, /*string*/ url){
  110. // summary:
  111. // generate the local bind methods for the remote object
  112. // method: string
  113. // The name of the method we are generating
  114. // parameters: array
  115. // the array of parameters for this call.
  116. // url: string
  117. // the service url for this call
  118. return dojo.hitch(this, function(){
  119. var deferredRequestHandler = new dojo.Deferred();
  120. // if params weren't specified, then we can assume it's varargs
  121. if( (this.strictArgChecks) &&
  122. (parameters != null) &&
  123. (arguments.length != parameters.length)
  124. ){
  125. // put error stuff here, no enough params
  126. throw new Error("Invalid number of parameters for remote method.");
  127. }else{
  128. this.bind(method, dojo._toArray(arguments), deferredRequestHandler, url);
  129. }
  130. return deferredRequestHandler;
  131. });
  132. },
  133. processSmd: function(object){
  134. // summary:
  135. // callback method for reciept of a smd object. Parse the smd
  136. // and generate functions based on the description
  137. // object:
  138. // smd object defining this service.
  139. if(object.methods){
  140. dojo.forEach(object.methods, function(m){
  141. if(m && m.name){
  142. this[m.name] = this.generateMethod( m.name,
  143. m.parameters,
  144. m.url||m.serviceUrl||m.serviceURL);
  145. if(!dojo.isFunction(this[m.name])){
  146. throw new Error("RpcService: Failed to create" + m.name + "()");
  147. /*console.log("RpcService: Failed to create", m.name, "()");*/
  148. }
  149. }
  150. }, this);
  151. }
  152. this.serviceUrl = object.serviceUrl||object.serviceURL;
  153. this.required = object.required;
  154. this.smd = object;
  155. }
  156. });
  157. }