JsonService.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. define("dojo/rpc/JsonService", ["../main", "./RpcService"], function(dojo) {
  2. // module:
  3. // dojo/rpc/JsonService
  4. // summary:
  5. // TODOC
  6. dojo.declare("dojo.rpc.JsonService", dojo.rpc.RpcService, {
  7. bustCache: false,
  8. contentType: "application/json-rpc",
  9. lastSubmissionId: 0,
  10. callRemote: function(method, params){
  11. // summary:
  12. // call an arbitrary remote method without requiring it to be
  13. // predefined with SMD
  14. // method: string
  15. // the name of the remote method you want to call.
  16. // params: array
  17. // array of parameters to pass to method
  18. var deferred = new dojo.Deferred();
  19. this.bind(method, params, deferred);
  20. return deferred;
  21. },
  22. bind: function(method, parameters, deferredRequestHandler, url){
  23. //summary:
  24. // JSON-RPC bind method. Takes remote method, parameters,
  25. // deferred, and a url, calls createRequest to make a JSON-RPC
  26. // envelope and passes that off with bind.
  27. // method: string
  28. // The name of the method we are calling
  29. // parameters: array
  30. // The parameters we are passing off to the method
  31. // deferredRequestHandler: deferred
  32. // The Deferred object for this particular request
  33. var def = dojo.rawXhrPost({
  34. url: url||this.serviceUrl,
  35. postData: this.createRequest(method, parameters),
  36. contentType: this.contentType,
  37. timeout: this.timeout,
  38. handleAs: "json-comment-optional"
  39. });
  40. def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler));
  41. },
  42. createRequest: function(method, params){
  43. // summary:
  44. // create a JSON-RPC envelope for the request
  45. // method: string
  46. // The name of the method we are creating the requst for
  47. // params: array
  48. // The array of parameters for this request;
  49. var req = { "params": params, "method": method, "id": ++this.lastSubmissionId };
  50. return dojo.toJson(req);
  51. },
  52. parseResults: function(/*anything*/obj){
  53. //summary:
  54. // parse the result envelope and pass the results back to
  55. // the callback function
  56. // obj: Object
  57. // Object containing envelope of data we recieve from the server
  58. if(dojo.isObject(obj)){
  59. if("result" in obj){
  60. return obj.result;
  61. }
  62. if("Result" in obj){
  63. return obj.Result;
  64. }
  65. if("ResultSet" in obj){
  66. return obj.ResultSet;
  67. }
  68. }
  69. return obj;
  70. }
  71. }
  72. );
  73. return dojo.rpc.JsonService;
  74. });