JsonpService.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. define("dojo/rpc/JsonpService", ["../main", "./RpcService", "../io/script"], function(dojo) {
  2. // module:
  3. // dojo/rpc/JsonpService
  4. // summary:
  5. // TODOC
  6. dojo.declare("dojo.rpc.JsonpService", dojo.rpc.RpcService, {
  7. // summary:
  8. // Generic JSONP service. Minimally extends RpcService to allow
  9. // easy definition of nearly any JSONP style service. Example
  10. // SMD files exist in dojox.data
  11. constructor: function(args, requiredArgs){
  12. if(this.required) {
  13. if(requiredArgs){
  14. dojo.mixin(this.required, requiredArgs);
  15. }
  16. dojo.forEach(this.required, function(req){
  17. if(req=="" || req==undefined){
  18. throw new Error("Required Service Argument not found: "+req);
  19. }
  20. });
  21. }
  22. },
  23. strictArgChecks: false,
  24. bind: function(method, parameters, deferredRequestHandler, url){
  25. //summary:
  26. // JSONP bind method. Takes remote method, parameters,
  27. // deferred, and a url, calls createRequest to make a JSON-RPC
  28. // envelope and passes that off with bind.
  29. // method: string
  30. // The name of the method we are calling
  31. // parameters: array
  32. // The parameters we are passing off to the method
  33. // deferredRequestHandler: deferred
  34. // The Deferred object for this particular request
  35. var def = dojo.io.script.get({
  36. url: url||this.serviceUrl,
  37. callbackParamName: this.callbackParamName||"callback",
  38. content: this.createRequest(parameters),
  39. timeout: this.timeout,
  40. handleAs: "json",
  41. preventCache: true
  42. });
  43. def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler));
  44. },
  45. createRequest: function(parameters){
  46. // summary:
  47. // create a JSONP req
  48. // params: array
  49. // The array of parameters for this request;
  50. var params = (dojo.isArrayLike(parameters) && parameters.length==1) ?
  51. parameters[0] : {};
  52. dojo.mixin(params,this.required);
  53. return params;
  54. }
  55. });
  56. return dojo.rpc.JsonpService;
  57. });