Requires

Provides

Request.JSONP.js

Defines Request.JSONP, a class for cross domain javascript via script injection.

License:
MIT-style license
Authors:
Aaron Newton, Guillermo Rauch, Arian Stolwijk
  1. 27
  2. 28
  3. 29
  4. 30
  5. 31
Request.JSONP = new Class({ Implements: [Chain, Events, Options], options: {

onRequest: function(src, scriptElement){}, onComplete: function(data){}, onSuccess: function(data){}, onCancel: function(){}, onTimeout: function(){}, onError: function(){},

  1. 39
  2. 40
  3. 41
  4. 42
  5. 43
  6. 44
  7. 45
  8. 46
  9. 47
  10. 48
  11. 49
  12. 50
  13. 51
  14. 52
  15. 53
  16. 54
  17. 55
  18. 56
  19. 57
  20. 58
  21. 59
  22. 60
  23. 61
  24. 62
  25. 63
  26. 64
  27. 65
  28. 66
  29. 67
  30. 68
  31. 69
  32. 70
  33. 71
  34. 72
  35. 73
  36. 74
  37. 75
  38. 76
  39. 77
  40. 78
  41. 79
  42. 80
  43. 81
  44. 82
  45. 83
  46. 84
  47. 85
  48. 86
  49. 87
  50. 88
  51. 89
  52. 90
  53. 91
  54. 92
  55. 93
  56. 94
  57. 95
  58. 96
  59. 97
  60. 98
  61. 99
  62. 100
  63. 101
  64. 102
  65. 103
  66. 104
  67. 105
  68. 106
  69. 107
  70. 108
  71. 109
  72. 110
  73. 111
  74. 112
  75. 113
  76. 114
  77. 115
  78. 116
  79. 117
  80. 118
  81. 119
  82. 120
  83. 121
  84. 122
  85. 123
  86. 124
  87. 125
  88. 126
  89. 127
  90. 128
  91. 129
onRequest: function(src){ if (this.options.log && window.console && console.log){ console.log('JSONP retrieving script with url:' + src); } }, onError: function(src){ if (this.options.log && window.console && console.warn){ console.warn('JSONP '+ src +' will fail in Internet Explorer, which enforces a 2083 bytes length limit on URIs'); } }, url: '', callbackKey: 'callback', injectScript: document.head, data: {}, link: 'ignore', timeout: 0, log: false }, initialize: function(options){ this.setOptions(options); }, send: function(options){ if (!Request.prototype.check.call(this, options)) return this; this.running = true; var type = typeOf(options); if (type == 'string' || type == 'element') options = {data: options}; options = Object.merge(this.options, options || {}); var data = options.data; switch (typeOf(data)){ case 'element': data = document.id(data).toQueryString(); break; case 'object': case 'hash': data = Object.toQueryString(data); } var index = this.index = Request.JSONP.counter++; var src = options.url + (options.url.test('\\?') ? '&' :'?') + (options.callbackKey) + '=Request.JSONP.request_map.request_'+ index + (data ? '&' + data : ''); if (src.length > 2083) this.fireEvent('error', src); var script = this.getScript(src).inject(options.injectScript); this.fireEvent('request', [script.get('src'), script]); Request.JSONP.request_map['request_' + index] = function(){ this.success(arguments, index); }.bind(this); if (options.timeout){ (function(){ this.cancel().fireEvent('timeout', [script.get('src'), script]); }).delay(options.timeout, this); } return this; }, getScript: function(src){ return this.script = new Element('script', { type: 'text/javascript', src: src }); }, success: function(args, index){ this.clear() .fireEvent('complete', args).fireEvent('success', args) .callChain(); }, cancel: function(){ return this.clear().fireEvent('cancel'); }, clear: function(){ if (this.script) this.script.destroy(); this.running = false; return this; } }); Request.JSONP.counter = 0; Request.JSONP.request_map = {};