JsonpService.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. define([
  2. "../_base/array", "../_base/declare", "../_base/lang", "./RpcService", "../io/script"],
  3. function(array, declare, lang, RpcService, script){
  4. // module:
  5. // dojo/rpc/JsonpService
  6. return declare("dojo.rpc.JsonpService", 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. lang.mixin(this.required, requiredArgs);
  15. }
  16. array.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 = 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 = (lang.isArrayLike(parameters) && parameters.length==1) ?
  51. parameters[0] : {};
  52. lang.mixin(params,this.required);
  53. return params;
  54. }
  55. });
  56. });