JsonpService.js 2.3 KB

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