JsonService.js 2.6 KB

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