/ext-4.1.0_b3/docs/source/JsonP.html

https://bitbucket.org/srogerf/javascript · HTML · 273 lines · 244 code · 29 blank · 0 comment · 0 complexity · 383d227e9cfe61851d6a1f8a4909758d MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-JsonP'>/**
  19. </span> * @class Ext.data.JsonP
  20. * @singleton
  21. * This class is used to create JSONP requests. JSONP is a mechanism that allows for making
  22. * requests for data cross domain. More information is available &lt;a href=&quot;http://en.wikipedia.org/wiki/JSONP&quot;&gt;here&lt;/a&gt;.
  23. */
  24. Ext.define('Ext.data.JsonP', {
  25. /* Begin Definitions */
  26. singleton: true,
  27. statics: {
  28. requestCount: 0,
  29. requests: {}
  30. },
  31. /* End Definitions */
  32. <span id='Ext-data-JsonP-property-timeout'> /**
  33. </span> * @property timeout
  34. * @type Number
  35. * A default timeout for any JsonP requests. If the request has not completed in this time the
  36. * failure callback will be fired. The timeout is in ms. Defaults to &lt;tt&gt;30000&lt;/tt&gt;.
  37. */
  38. timeout: 30000,
  39. <span id='Ext-data-JsonP-property-disableCaching'> /**
  40. </span> * @property disableCaching
  41. * @type Boolean
  42. * True to add a unique cache-buster param to requests. Defaults to &lt;tt&gt;true&lt;/tt&gt;.
  43. */
  44. disableCaching: true,
  45. <span id='Ext-data-JsonP-property-disableCachingParam'> /**
  46. </span> * @property disableCachingParam
  47. * @type String
  48. * Change the parameter which is sent went disabling caching through a cache buster. Defaults to &lt;tt&gt;'_dc'&lt;/tt&gt;.
  49. */
  50. disableCachingParam: '_dc',
  51. <span id='Ext-data-JsonP-property-callbackKey'> /**
  52. </span> * @property callbackKey
  53. * @type String
  54. * Specifies the GET parameter that will be sent to the server containing the function name to be executed when
  55. * the request completes. Defaults to &lt;tt&gt;callback&lt;/tt&gt;. Thus, a common request will be in the form of
  56. * url?callback=Ext.data.JsonP.callback1
  57. */
  58. callbackKey: 'callback',
  59. <span id='Ext-data-JsonP-method-request'> /**
  60. </span> * Makes a JSONP request.
  61. * @param {Object} options An object which may contain the following properties. Note that options will
  62. * take priority over any defaults that are specified in the class.
  63. * &lt;ul&gt;
  64. * &lt;li&gt;&lt;b&gt;url&lt;/b&gt; : String &lt;div class=&quot;sub-desc&quot;&gt;The URL to request.&lt;/div&gt;&lt;/li&gt;
  65. * &lt;li&gt;&lt;b&gt;params&lt;/b&gt; : Object (Optional)&lt;div class=&quot;sub-desc&quot;&gt;An object containing a series of
  66. * key value pairs that will be sent along with the request.&lt;/div&gt;&lt;/li&gt;
  67. * &lt;li&gt;&lt;b&gt;timeout&lt;/b&gt; : Number (Optional) &lt;div class=&quot;sub-desc&quot;&gt;See {@link #timeout}&lt;/div&gt;&lt;/li&gt;
  68. * &lt;li&gt;&lt;b&gt;callbackKey&lt;/b&gt; : String (Optional) &lt;div class=&quot;sub-desc&quot;&gt;See {@link #callbackKey}&lt;/div&gt;&lt;/li&gt;
  69. * &lt;li&gt;&lt;b&gt;callbackName&lt;/b&gt; : String (Optional) &lt;div class=&quot;sub-desc&quot;&gt;The function name to use for this request.
  70. * By default this name will be auto-generated: Ext.data.JsonP.callback1, Ext.data.JsonP.callback2, etc.
  71. * Setting this option to &quot;my_name&quot; will force the function name to be Ext.data.JsonP.my_name.
  72. * Use this if you want deterministic behavior, but be careful - the callbackName should be different
  73. * in each JsonP request that you make.&lt;/div&gt;&lt;/li&gt;
  74. * &lt;li&gt;&lt;b&gt;disableCaching&lt;/b&gt; : Boolean (Optional) &lt;div class=&quot;sub-desc&quot;&gt;See {@link #disableCaching}&lt;/div&gt;&lt;/li&gt;
  75. * &lt;li&gt;&lt;b&gt;disableCachingParam&lt;/b&gt; : String (Optional) &lt;div class=&quot;sub-desc&quot;&gt;See {@link #disableCachingParam}&lt;/div&gt;&lt;/li&gt;
  76. * &lt;li&gt;&lt;b&gt;success&lt;/b&gt; : Function (Optional) &lt;div class=&quot;sub-desc&quot;&gt;A function to execute if the request succeeds.&lt;/div&gt;&lt;/li&gt;
  77. * &lt;li&gt;&lt;b&gt;failure&lt;/b&gt; : Function (Optional) &lt;div class=&quot;sub-desc&quot;&gt;A function to execute if the request fails.&lt;/div&gt;&lt;/li&gt;
  78. * &lt;li&gt;&lt;b&gt;callback&lt;/b&gt; : Function (Optional) &lt;div class=&quot;sub-desc&quot;&gt;A function to execute when the request
  79. * completes, whether it is a success or failure.&lt;/div&gt;&lt;/li&gt;
  80. * &lt;li&gt;&lt;b&gt;scope&lt;/b&gt; : Object (Optional)&lt;div class=&quot;sub-desc&quot;&gt;The scope in
  81. * which to execute the callbacks: The &quot;this&quot; object for the callback function. Defaults to the browser window.&lt;/div&gt;&lt;/li&gt;
  82. * &lt;/ul&gt;
  83. * @return {Object} request An object containing the request details.
  84. */
  85. request: function(options){
  86. options = Ext.apply({}, options);
  87. //&lt;debug&gt;
  88. if (!options.url) {
  89. Ext.Error.raise('A url must be specified for a JSONP request.');
  90. }
  91. //&lt;/debug&gt;
  92. var me = this,
  93. disableCaching = Ext.isDefined(options.disableCaching) ? options.disableCaching : me.disableCaching,
  94. cacheParam = options.disableCachingParam || me.disableCachingParam,
  95. id = ++me.statics().requestCount,
  96. callbackName = options.callbackName || 'callback' + id,
  97. callbackKey = options.callbackKey || me.callbackKey,
  98. timeout = Ext.isDefined(options.timeout) ? options.timeout : me.timeout,
  99. params = Ext.apply({}, options.params),
  100. url = options.url,
  101. name = Ext.name,
  102. request,
  103. script;
  104. params[callbackKey] = name + '.data.JsonP.' + callbackName;
  105. if (disableCaching) {
  106. params[cacheParam] = new Date().getTime();
  107. }
  108. script = me.createScript(url, params, options);
  109. me.statics().requests[id] = request = {
  110. url: url,
  111. params: params,
  112. script: script,
  113. id: id,
  114. scope: options.scope,
  115. success: options.success,
  116. failure: options.failure,
  117. callback: options.callback,
  118. callbackKey: callbackKey,
  119. callbackName: callbackName
  120. };
  121. if (timeout &gt; 0) {
  122. request.timeout = setTimeout(Ext.bind(me.handleTimeout, me, [request]), timeout);
  123. }
  124. me.setupErrorHandling(request);
  125. me[callbackName] = Ext.bind(me.handleResponse, me, [request], true);
  126. me.loadScript(request);
  127. return request;
  128. },
  129. <span id='Ext-data-JsonP-method-abort'> /**
  130. </span> * Abort a request. If the request parameter is not specified all open requests will
  131. * be aborted.
  132. * @param {Object/String} request (Optional) The request to abort
  133. */
  134. abort: function(request){
  135. var me = this,
  136. requests = me.statics().requests,
  137. key;
  138. if (request) {
  139. if (!request.id) {
  140. request = requests[request];
  141. }
  142. me.handleAbort(request);
  143. } else {
  144. for (key in requests) {
  145. if (requests.hasOwnProperty(key)) {
  146. me.abort(requests[key]);
  147. }
  148. }
  149. }
  150. },
  151. <span id='Ext-data-JsonP-method-setupErrorHandling'> /**
  152. </span> * Sets up error handling for the script
  153. * @private
  154. * @param {Object} request The request
  155. */
  156. setupErrorHandling: function(request){
  157. request.script.onerror = Ext.bind(this.handleError, this, [request]);
  158. },
  159. <span id='Ext-data-JsonP-method-handleAbort'> /**
  160. </span> * Handles any aborts when loading the script
  161. * @private
  162. * @param {Object} request The request
  163. */
  164. handleAbort: function(request){
  165. request.errorType = 'abort';
  166. this.handleResponse(null, request);
  167. },
  168. <span id='Ext-data-JsonP-method-handleError'> /**
  169. </span> * Handles any script errors when loading the script
  170. * @private
  171. * @param {Object} request The request
  172. */
  173. handleError: function(request){
  174. request.errorType = 'error';
  175. this.handleResponse(null, request);
  176. },
  177. <span id='Ext-data-JsonP-method-cleanupErrorHandling'> /**
  178. </span> * Cleans up anu script handling errors
  179. * @private
  180. * @param {Object} request The request
  181. */
  182. cleanupErrorHandling: function(request){
  183. request.script.onerror = null;
  184. },
  185. <span id='Ext-data-JsonP-method-handleTimeout'> /**
  186. </span> * Handle any script timeouts
  187. * @private
  188. * @param {Object} request The request
  189. */
  190. handleTimeout: function(request){
  191. request.errorType = 'timeout';
  192. this.handleResponse(null, request);
  193. },
  194. <span id='Ext-data-JsonP-method-handleResponse'> /**
  195. </span> * Handle a successful response
  196. * @private
  197. * @param {Object} result The result from the request
  198. * @param {Object} request The request
  199. */
  200. handleResponse: function(result, request){
  201. var success = true;
  202. if (request.timeout) {
  203. clearTimeout(request.timeout);
  204. }
  205. delete this[request.callbackName];
  206. delete this.statics().requests[request.id];
  207. this.cleanupErrorHandling(request);
  208. Ext.fly(request.script).remove();
  209. if (request.errorType) {
  210. success = false;
  211. Ext.callback(request.failure, request.scope, [request.errorType]);
  212. } else {
  213. Ext.callback(request.success, request.scope, [result]);
  214. }
  215. Ext.callback(request.callback, request.scope, [success, result, request.errorType]);
  216. },
  217. <span id='Ext-data-JsonP-method-createScript'> /**
  218. </span> * Create the script tag given the specified url, params and options. The options
  219. * parameter is passed to allow an override to access it.
  220. * @private
  221. * @param {String} url The url of the request
  222. * @param {Object} params Any extra params to be sent
  223. * @param {Object} options The object passed to {@link #request}.
  224. */
  225. createScript: function(url, params, options) {
  226. var script = document.createElement('script');
  227. script.setAttribute(&quot;src&quot;, Ext.urlAppend(url, Ext.Object.toQueryString(params)));
  228. script.setAttribute(&quot;async&quot;, true);
  229. script.setAttribute(&quot;type&quot;, &quot;text/javascript&quot;);
  230. return script;
  231. },
  232. <span id='Ext-data-JsonP-method-loadScript'> /**
  233. </span> * Loads the script for the given request by appending it to the HEAD element. This is
  234. * its own method so that users can override it (as well as {@link #createScript}).
  235. * @private
  236. * @param request The request object.
  237. */
  238. loadScript: function (request) {
  239. Ext.getHead().appendChild(request.script);
  240. }
  241. });
  242. </pre>
  243. </body>
  244. </html>