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; }
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 <a href="http://en.wikipedia.org/wiki/JSONP">here</a>.
24 Ext.define('Ext.data.JsonP', {
26 /* Begin Definitions */
37 <span id='Ext-data-JsonP-property-timeout'> /**
38 </span> * @property timeout
40 * A default timeout for any JsonP requests. If the request has not completed in this time the
41 * failure callback will be fired. The timeout is in ms. Defaults to <tt>30000</tt>.
45 <span id='Ext-data-JsonP-property-disableCaching'> /**
46 </span> * @property disableCaching
48 * True to add a unique cache-buster param to requests. Defaults to <tt>true</tt>.
52 <span id='Ext-data-JsonP-property-disableCachingParam'> /**
53 </span> * @property disableCachingParam
55 * Change the parameter which is sent went disabling caching through a cache buster. Defaults to <tt>'_dc'</tt>.
57 disableCachingParam: '_dc',
59 <span id='Ext-data-JsonP-property-callbackKey'> /**
60 </span> * @property callbackKey
62 * Specifies the GET parameter that will be sent to the server containing the function name to be executed when
63 * the request completes. Defaults to <tt>callback</tt>. Thus, a common request will be in the form of
64 * url?callback=Ext.data.JsonP.callback1
66 callbackKey: 'callback',
68 <span id='Ext-data-JsonP-method-request'> /**
69 </span> * Makes a JSONP request.
70 * @param {Object} options An object which may contain the following properties. Note that options will
71 * take priority over any defaults that are specified in the class.
73 * <li><b>url</b> : String <div class="sub-desc">The URL to request.</div></li>
74 * <li><b>params</b> : Object (Optional)<div class="sub-desc">An object containing a series of
75 * key value pairs that will be sent along with the request.</div></li>
76 * <li><b>timeout</b> : Number (Optional) <div class="sub-desc">See {@link #timeout}</div></li>
77 * <li><b>callbackKey</b> : String (Optional) <div class="sub-desc">See {@link #callbackKey}</div></li>
78 * <li><b>callbackName</b> : String (Optional) <div class="sub-desc">The function name to use for this request.
79 * By default this name will be auto-generated: Ext.data.JsonP.callback1, Ext.data.JsonP.callback2, etc.
80 * Setting this option to "my_name" will force the function name to be Ext.data.JsonP.my_name.
81 * Use this if you want deterministic behavior, but be careful - the callbackName should be different
82 * in each JsonP request that you make.</div></li>
83 * <li><b>disableCaching</b> : Boolean (Optional) <div class="sub-desc">See {@link #disableCaching}</div></li>
84 * <li><b>disableCachingParam</b> : String (Optional) <div class="sub-desc">See {@link #disableCachingParam}</div></li>
85 * <li><b>success</b> : Function (Optional) <div class="sub-desc">A function to execute if the request succeeds.</div></li>
86 * <li><b>failure</b> : Function (Optional) <div class="sub-desc">A function to execute if the request fails.</div></li>
87 * <li><b>callback</b> : Function (Optional) <div class="sub-desc">A function to execute when the request
88 * completes, whether it is a success or failure.</div></li>
89 * <li><b>scope</b> : Object (Optional)<div class="sub-desc">The scope in
90 * which to execute the callbacks: The "this" object for the callback function. Defaults to the browser window.</div></li>
92 * @return {Object} request An object containing the request details.
94 request: function(options){
95 options = Ext.apply({}, options);
99 Ext.Error.raise('A url must be specified for a JSONP request.');
104 disableCaching = Ext.isDefined(options.disableCaching) ? options.disableCaching : me.disableCaching,
105 cacheParam = options.disableCachingParam || me.disableCachingParam,
106 id = ++me.statics().requestCount,
107 callbackName = options.callbackName || 'callback' + id,
108 callbackKey = options.callbackKey || me.callbackKey,
109 timeout = Ext.isDefined(options.timeout) ? options.timeout : me.timeout,
110 params = Ext.apply({}, options.params),
112 name = Ext.isSandboxed ? Ext.getUniqueGlobalNamespace() : 'Ext',
116 params[callbackKey] = name + '.data.JsonP.' + callbackName;
117 if (disableCaching) {
118 params[cacheParam] = new Date().getTime();
121 script = me.createScript(url, params);
123 me.statics().requests[id] = request = {
128 scope: options.scope,
129 success: options.success,
130 failure: options.failure,
131 callback: options.callback,
132 callbackName: callbackName
135 if (timeout > 0) {
136 request.timeout = setTimeout(Ext.bind(me.handleTimeout, me, [request]), timeout);
139 me.setupErrorHandling(request);
140 me[callbackName] = Ext.bind(me.handleResponse, me, [request], true);
141 Ext.getHead().appendChild(script);
145 <span id='Ext-data-JsonP-method-abort'> /**
146 </span> * Abort a request. If the request parameter is not specified all open requests will
148 * @param {Object/String} request (Optional) The request to abort
150 abort: function(request){
151 var requests = this.statics().requests,
156 request = requests[request];
160 for (key in requests) {
161 if (requests.hasOwnProperty(key)) {
162 this.abort(requests[key]);
168 <span id='Ext-data-JsonP-method-setupErrorHandling'> /**
169 </span> * Sets up error handling for the script
171 * @param {Object} request The request
173 setupErrorHandling: function(request){
174 request.script.onerror = Ext.bind(this.handleError, this, [request]);
177 <span id='Ext-data-JsonP-method-handleAbort'> /**
178 </span> * Handles any aborts when loading the script
180 * @param {Object} request The request
182 handleAbort: function(request){
183 request.errorType = 'abort';
184 this.handleResponse(null, request);
187 <span id='Ext-data-JsonP-method-handleError'> /**
188 </span> * Handles any script errors when loading the script
190 * @param {Object} request The request
192 handleError: function(request){
193 request.errorType = 'error';
194 this.handleResponse(null, request);
197 <span id='Ext-data-JsonP-method-cleanupErrorHandling'> /**
198 </span> * Cleans up anu script handling errors
200 * @param {Object} request The request
202 cleanupErrorHandling: function(request){
203 request.script.onerror = null;
206 <span id='Ext-data-JsonP-method-handleTimeout'> /**
207 </span> * Handle any script timeouts
209 * @param {Object} request The request
211 handleTimeout: function(request){
212 request.errorType = 'timeout';
213 this.handleResponse(null, request);
216 <span id='Ext-data-JsonP-method-handleResponse'> /**
217 </span> * Handle a successful response
219 * @param {Object} result The result from the request
220 * @param {Object} request The request
222 handleResponse: function(result, request){
226 if (request.timeout) {
227 clearTimeout(request.timeout);
229 delete this[request.callbackName];
230 delete this.statics()[request.id];
231 this.cleanupErrorHandling(request);
232 Ext.fly(request.script).remove();
234 if (request.errorType) {
236 Ext.callback(request.failure, request.scope, [request.errorType]);
238 Ext.callback(request.success, request.scope, [result]);
240 Ext.callback(request.callback, request.scope, [success, result, request.errorType]);
243 <span id='Ext-data-JsonP-method-createScript'> /**
244 </span> * Create the script tag
246 * @param {String} url The url of the request
247 * @param {Object} params Any extra params to be sent
249 createScript: function(url, params) {
250 var script = document.createElement('script');
251 script.setAttribute("src", Ext.urlAppend(url, Ext.Object.toQueryString(params)));
252 script.setAttribute("async", true);
253 script.setAttribute("type", "text/javascript");