1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.JsonP'>/**
2 </span> * @class Ext.data.JsonP
4 * This class is used to create JSONP requests. JSONP is a mechanism that allows for making
5 * requests for data cross domain. More information is available here:
6 * http://en.wikipedia.org/wiki/JSONP
8 Ext.define('Ext.data.JsonP', {
10 /* Begin Definitions */
21 <span id='Ext-data.JsonP-property-timeout'> /**
22 </span> * @property timeout
24 * A default timeout for any JsonP requests. If the request has not completed in this time the
25 * failure callback will be fired. The timeout is in ms. Defaults to <tt>30000</tt>.
29 <span id='Ext-data.JsonP-property-disableCaching'> /**
30 </span> * @property disableCaching
32 * True to add a unique cache-buster param to requests. Defaults to <tt>true</tt>.
36 <span id='Ext-data.JsonP-property-disableCachingParam'> /**
37 </span> * @property disableCachingParam
39 * Change the parameter which is sent went disabling caching through a cache buster. Defaults to <tt>'_dc'</tt>.
41 disableCachingParam: '_dc',
43 <span id='Ext-data.JsonP-property-callbackKey'> /**
44 </span> * @property callbackKey
46 * Specifies the GET parameter that will be sent to the server containing the function name to be executed when
47 * the request completes. Defaults to <tt>callback</tt>. Thus, a common request will be in the form of
48 * url?callback=Ext.data.JsonP.callback1
50 callbackKey: 'callback',
52 <span id='Ext-data.JsonP-method-request'> /**
53 </span> * Makes a JSONP request.
54 * @param {Object} options An object which may contain the following properties. Note that options will
55 * take priority over any defaults that are specified in the class.
57 * <li><b>url</b> : String <div class="sub-desc">The URL to request.</div></li>
58 * <li><b>params</b> : Object (Optional)<div class="sub-desc">An object containing a series of
59 * key value pairs that will be sent along with the request.</div></li>
60 * <li><b>timeout</b> : Number (Optional) <div class="sub-desc">See {@link #timeout}</div></li>
61 * <li><b>callbackKey</b> : String (Optional) <div class="sub-desc">See {@link #callbackKey}</div></li>
62 * <li><b>disableCaching</b> : Boolean (Optional) <div class="sub-desc">See {@link #disableCaching}</div></li>
63 * <li><b>disableCachingParam</b> : String (Optional) <div class="sub-desc">See {@link #disableCachingParam}</div></li>
64 * <li><b>success</b> : Function (Optional) <div class="sub-desc">A function to execute if the request succeeds.</div></li>
65 * <li><b>failure</b> : Function (Optional) <div class="sub-desc">A function to execute if the request fails.</div></li>
66 * <li><b>callback</b> : Function (Optional) <div class="sub-desc">A function to execute when the request
67 * completes, whether it is a success or failure.</div></li>
68 * <li><b>scope</b> : Object (Optional)<div class="sub-desc">The scope in
69 * which to execute the callbacks: The "this" object for the callback function. Defaults to the browser window.</div></li>
71 * @return {Object} request An object containing the request details.
73 request: function(options){
74 options = Ext.apply({}, options);
78 Ext.Error.raise('A url must be specified for a JSONP request.');
83 disableCaching = Ext.isDefined(options.disableCaching) ? options.disableCaching : me.disableCaching,
84 cacheParam = options.disableCachingParam || me.disableCachingParam,
85 id = ++me.statics().requestCount,
86 callbackName = 'callback' + id,
87 callbackKey = options.callbackKey || me.callbackKey,
88 timeout = Ext.isDefined(options.timeout) ? options.timeout : me.timeout,
89 params = Ext.apply({}, options.params),
94 params[callbackKey] = 'Ext.data.JsonP.' + callbackName;
96 params[cacheParam] = new Date().getTime();
99 script = me.createScript(url, params);
101 me.statics().requests[id] = request = {
106 scope: options.scope,
107 success: options.success,
108 failure: options.failure,
109 callback: options.callback,
110 callbackName: callbackName
113 if (timeout > 0) {
114 request.timeout = setTimeout(Ext.bind(me.handleTimeout, me, [request]), timeout);
117 me.setupErrorHandling(request);
118 me[callbackName] = Ext.bind(me.handleResponse, me, [request], true);
119 Ext.getHead().appendChild(script);
123 <span id='Ext-data.JsonP-method-abort'> /**
124 </span> * Abort a request. If the request parameter is not specified all open requests will
126 * @param {Object/String} request (Optional) The request to abort
128 abort: function(request){
129 var requests = this.statics().requests,
134 request = requests[request];
138 for (key in requests) {
139 if (requests.hasOwnProperty(key)) {
140 this.abort(requests[key]);
146 <span id='Ext-data.JsonP-method-setupErrorHandling'> /**
147 </span> * Sets up error handling for the script
149 * @param {Object} request The request
151 setupErrorHandling: function(request){
152 request.script.onerror = Ext.bind(this.handleError, this, [request]);
155 <span id='Ext-data.JsonP-method-handleAbort'> /**
156 </span> * Handles any aborts when loading the script
158 * @param {Object} request The request
160 handleAbort: function(request){
161 request.errorType = 'abort';
162 this.handleResponse(null, request);
165 <span id='Ext-data.JsonP-method-handleError'> /**
166 </span> * Handles any script errors when loading the script
168 * @param {Object} request The request
170 handleError: function(request){
171 request.errorType = 'error';
172 this.handleResponse(null, request);
175 <span id='Ext-data.JsonP-method-cleanupErrorHandling'> /**
176 </span> * Cleans up anu script handling errors
178 * @param {Object} request The request
180 cleanupErrorHandling: function(request){
181 request.script.onerror = null;
184 <span id='Ext-data.JsonP-method-handleTimeout'> /**
185 </span> * Handle any script timeouts
187 * @param {Object} request The request
189 handleTimeout: function(request){
190 request.errorType = 'timeout';
191 this.handleResponse(null, request);
194 <span id='Ext-data.JsonP-method-handleResponse'> /**
195 </span> * Handle a successful response
197 * @param {Object} result The result from the request
198 * @param {Object} request The request
200 handleResponse: function(result, request){
204 if (request.timeout) {
205 clearTimeout(request.timeout);
207 delete this[request.callbackName];
208 delete this.statics()[request.id];
209 this.cleanupErrorHandling(request);
210 Ext.fly(request.script).remove();
212 if (request.errorType) {
214 Ext.callback(request.failure, request.scope, [request.errorType]);
216 Ext.callback(request.success, request.scope, [result]);
218 Ext.callback(request.callback, request.scope, [success, result, request.errorType]);
221 <span id='Ext-data.JsonP-method-createScript'> /**
222 </span> * Create the script tag
224 * @param {String} url The url of the request
225 * @param {Object} params Any extra params to be sent
227 createScript: function(url, params) {
228 var script = document.createElement('script');
229 script.setAttribute("src", Ext.urlAppend(url, Ext.Object.toQueryString(params)));
230 script.setAttribute("async", true);
231 script.setAttribute("type", "text/javascript");
235 </pre></pre></body></html>