4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-JsonP'>/**
19 </span> * @class Ext.data.JsonP
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 here:
23 * http://en.wikipedia.org/wiki/JSONP
25 Ext.define('Ext.data.JsonP', {
27 /* Begin Definitions */
38 <span id='Ext-data-JsonP-property-timeout'> /**
39 </span> * @property timeout
41 * A default timeout for any JsonP requests. If the request has not completed in this time the
42 * failure callback will be fired. The timeout is in ms. Defaults to <tt>30000</tt>.
46 <span id='Ext-data-JsonP-property-disableCaching'> /**
47 </span> * @property disableCaching
49 * True to add a unique cache-buster param to requests. Defaults to <tt>true</tt>.
53 <span id='Ext-data-JsonP-property-disableCachingParam'> /**
54 </span> * @property disableCachingParam
56 * Change the parameter which is sent went disabling caching through a cache buster. Defaults to <tt>'_dc'</tt>.
58 disableCachingParam: '_dc',
60 <span id='Ext-data-JsonP-property-callbackKey'> /**
61 </span> * @property callbackKey
63 * Specifies the GET parameter that will be sent to the server containing the function name to be executed when
64 * the request completes. Defaults to <tt>callback</tt>. Thus, a common request will be in the form of
65 * url?callback=Ext.data.JsonP.callback1
67 callbackKey: 'callback',
69 <span id='Ext-data-JsonP-method-request'> /**
70 </span> * Makes a JSONP request.
71 * @param {Object} options An object which may contain the following properties. Note that options will
72 * take priority over any defaults that are specified in the class.
74 * <li><b>url</b> : String <div class="sub-desc">The URL to request.</div></li>
75 * <li><b>params</b> : Object (Optional)<div class="sub-desc">An object containing a series of
76 * key value pairs that will be sent along with the request.</div></li>
77 * <li><b>timeout</b> : Number (Optional) <div class="sub-desc">See {@link #timeout}</div></li>
78 * <li><b>callbackKey</b> : String (Optional) <div class="sub-desc">See {@link #callbackKey}</div></li>
79 * <li><b>disableCaching</b> : Boolean (Optional) <div class="sub-desc">See {@link #disableCaching}</div></li>
80 * <li><b>disableCachingParam</b> : String (Optional) <div class="sub-desc">See {@link #disableCachingParam}</div></li>
81 * <li><b>success</b> : Function (Optional) <div class="sub-desc">A function to execute if the request succeeds.</div></li>
82 * <li><b>failure</b> : Function (Optional) <div class="sub-desc">A function to execute if the request fails.</div></li>
83 * <li><b>callback</b> : Function (Optional) <div class="sub-desc">A function to execute when the request
84 * completes, whether it is a success or failure.</div></li>
85 * <li><b>scope</b> : Object (Optional)<div class="sub-desc">The scope in
86 * which to execute the callbacks: The "this" object for the callback function. Defaults to the browser window.</div></li>
88 * @return {Object} request An object containing the request details.
90 request: function(options){
91 options = Ext.apply({}, options);
95 Ext.Error.raise('A url must be specified for a JSONP request.');
100 disableCaching = Ext.isDefined(options.disableCaching) ? options.disableCaching : me.disableCaching,
101 cacheParam = options.disableCachingParam || me.disableCachingParam,
102 id = ++me.statics().requestCount,
103 callbackName = 'callback' + id,
104 callbackKey = options.callbackKey || me.callbackKey,
105 timeout = Ext.isDefined(options.timeout) ? options.timeout : me.timeout,
106 params = Ext.apply({}, options.params),
111 params[callbackKey] = 'Ext.data.JsonP.' + callbackName;
112 if (disableCaching) {
113 params[cacheParam] = new Date().getTime();
116 script = me.createScript(url, params);
118 me.statics().requests[id] = request = {
123 scope: options.scope,
124 success: options.success,
125 failure: options.failure,
126 callback: options.callback,
127 callbackName: callbackName
130 if (timeout > 0) {
131 request.timeout = setTimeout(Ext.bind(me.handleTimeout, me, [request]), timeout);
134 me.setupErrorHandling(request);
135 me[callbackName] = Ext.bind(me.handleResponse, me, [request], true);
136 Ext.getHead().appendChild(script);
140 <span id='Ext-data-JsonP-method-abort'> /**
141 </span> * Abort a request. If the request parameter is not specified all open requests will
143 * @param {Object/String} request (Optional) The request to abort
145 abort: function(request){
146 var requests = this.statics().requests,
151 request = requests[request];
155 for (key in requests) {
156 if (requests.hasOwnProperty(key)) {
157 this.abort(requests[key]);
163 <span id='Ext-data-JsonP-method-setupErrorHandling'> /**
164 </span> * Sets up error handling for the script
166 * @param {Object} request The request
168 setupErrorHandling: function(request){
169 request.script.onerror = Ext.bind(this.handleError, this, [request]);
172 <span id='Ext-data-JsonP-method-handleAbort'> /**
173 </span> * Handles any aborts when loading the script
175 * @param {Object} request The request
177 handleAbort: function(request){
178 request.errorType = 'abort';
179 this.handleResponse(null, request);
182 <span id='Ext-data-JsonP-method-handleError'> /**
183 </span> * Handles any script errors when loading the script
185 * @param {Object} request The request
187 handleError: function(request){
188 request.errorType = 'error';
189 this.handleResponse(null, request);
192 <span id='Ext-data-JsonP-method-cleanupErrorHandling'> /**
193 </span> * Cleans up anu script handling errors
195 * @param {Object} request The request
197 cleanupErrorHandling: function(request){
198 request.script.onerror = null;
201 <span id='Ext-data-JsonP-method-handleTimeout'> /**
202 </span> * Handle any script timeouts
204 * @param {Object} request The request
206 handleTimeout: function(request){
207 request.errorType = 'timeout';
208 this.handleResponse(null, request);
211 <span id='Ext-data-JsonP-method-handleResponse'> /**
212 </span> * Handle a successful response
214 * @param {Object} result The result from the request
215 * @param {Object} request The request
217 handleResponse: function(result, request){
221 if (request.timeout) {
222 clearTimeout(request.timeout);
224 delete this[request.callbackName];
225 delete this.statics()[request.id];
226 this.cleanupErrorHandling(request);
227 Ext.fly(request.script).remove();
229 if (request.errorType) {
231 Ext.callback(request.failure, request.scope, [request.errorType]);
233 Ext.callback(request.success, request.scope, [result]);
235 Ext.callback(request.callback, request.scope, [success, result, request.errorType]);
238 <span id='Ext-data-JsonP-method-createScript'> /**
239 </span> * Create the script tag
241 * @param {String} url The url of the request
242 * @param {Object} params Any extra params to be sent
244 createScript: function(url, params) {
245 var script = document.createElement('script');
246 script.setAttribute("src", Ext.urlAppend(url, Ext.Object.toQueryString(params)));
247 script.setAttribute("async", true);
248 script.setAttribute("type", "text/javascript");