3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.data.ScriptTagProxy"></div>/**
\r
15 * @class Ext.data.ScriptTagProxy
\r
16 * @extends Ext.data.DataProxy
\r
17 * An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain
\r
18 * other than the originating domain of the running page.<br>
\r
20 * <b>Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain
\r
21 * of the running page, you must use this class, rather than HttpProxy.</b><br>
\r
23 * The content passed back from a server resource requested by a ScriptTagProxy <b>must</b> be executable JavaScript
\r
24 * source code because it is used as the source inside a <script> tag.<br>
\r
26 * In order for the browser to process the returned data, the server must wrap the data object
\r
27 * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.
\r
28 * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy
\r
29 * depending on whether the callback name was passed:
\r
32 boolean scriptTag = false;
\r
33 String cb = request.getParameter("callback");
\r
36 response.setContentType("text/javascript");
\r
38 response.setContentType("application/x-json");
\r
40 Writer out = response.getWriter();
\r
42 out.write(cb + "(");
\r
44 out.print(dataBlock.toJsonString());
\r
51 * @param {Object} config A configuration object.
\r
53 Ext.data.ScriptTagProxy = function(config){
\r
54 Ext.apply(this, config);
\r
56 Ext.data.ScriptTagProxy.superclass.constructor.call(this, config);
\r
58 this.head = document.getElementsByTagName("head")[0];
\r
60 <div id="event-Ext.data.ScriptTagProxy-loadexception"></div>/**
\r
61 * @event loadexception
\r
62 * <b>Deprecated</b> in favor of 'exception' event.
\r
63 * Fires if an exception occurs in the Proxy during data loading. This event can be fired for one of two reasons:
\r
64 * <ul><li><b>The load call timed out.</b> This means the load callback did not execute within the time limit
\r
65 * specified by {@link #timeout}. In this case, this event will be raised and the
\r
66 * fourth parameter (read error) will be null.</li>
\r
67 * <li><b>The load succeeded but the reader could not read the response.</b> This means the server returned
\r
68 * data, but the configured Reader threw an error while reading the data. In this case, this event will be
\r
69 * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>
\r
70 * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly
\r
71 * on any Store instance.
\r
72 * @param {Object} this
\r
73 * @param {Object} options The loading options that were specified (see {@link #load} for details). If the load
\r
74 * call timed out, this parameter will be null.
\r
75 * @param {Object} arg The callback's arg object passed to the {@link #load} function
\r
76 * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.
\r
77 * If the remote request returns success: false, this parameter will be null.
\r
81 Ext.data.ScriptTagProxy.TRANS_ID = 1000;
\r
83 Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {
\r
84 <div id="cfg-Ext.data.ScriptTagProxy-url"></div>/**
\r
85 * @cfg {String} url The URL from which to request the data object.
\r
87 <div id="cfg-Ext.data.ScriptTagProxy-timeout"></div>/**
\r
88 * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
\r
91 <div id="cfg-Ext.data.ScriptTagProxy-callbackParam"></div>/**
\r
92 * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
\r
93 * the server the name of the callback function set up by the load call to process the returned data object.
\r
94 * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
\r
95 * javascript output which calls this named function passing the data object as its only parameter.
\r
97 callbackParam : "callback",
\r
98 <div id="cfg-Ext.data.ScriptTagProxy-nocache"></div>/**
\r
99 * @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter
\r
100 * name to the request.
\r
104 <div id="method-Ext.data.ScriptTagProxy-doRequest"></div>/**
\r
105 * HttpProxy implementation of DataProxy#doRequest
\r
106 * @param {String} action
\r
107 * @param {Ext.data.Record/Ext.data.Record[]} rs If action is <tt>read</tt>, rs will be null
\r
108 * @param {Object} params An object containing properties which are to be used as HTTP parameters
\r
109 * for the request to the remote server.
\r
110 * @param {Ext.data.DataReader} reader The Reader object which converts the data
\r
111 * object into a block of Ext.data.Records.
\r
112 * @param {Function} callback The function into which to pass the block of Ext.data.Records.
\r
113 * The function must be passed <ul>
\r
114 * <li>The Record block object</li>
\r
115 * <li>The "arg" argument from the load function</li>
\r
116 * <li>A boolean success indicator</li>
\r
118 * @param {Object} scope The scope in which to call the callback
\r
119 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
\r
121 doRequest : function(action, rs, params, reader, callback, scope, arg) {
\r
122 var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
\r
124 var url = this.buildUrl(action, rs);
\r
126 throw new Ext.data.Api.Error('invalid-url', url);
\r
128 url = Ext.urlAppend(url, p);
\r
131 url = Ext.urlAppend(url, '_dc=' + (new Date().getTime()));
\r
133 var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
\r
137 cb : "stcCallback"+transId,
\r
138 scriptId : "stcScript"+transId,
\r
142 callback : callback,
\r
146 window[trans.cb] = this.createCallback(action, rs, trans);
\r
147 url += String.format("&{0}={1}", this.callbackParam, trans.cb);
\r
148 if(this.autoAbort !== false){
\r
152 trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
\r
154 var script = document.createElement("script");
\r
155 script.setAttribute("src", url);
\r
156 script.setAttribute("type", "text/javascript");
\r
157 script.setAttribute("id", trans.scriptId);
\r
158 this.head.appendChild(script);
\r
160 this.trans = trans;
\r
163 // @private createCallback
\r
164 createCallback : function(action, rs, trans) {
\r
166 return function(res) {
\r
167 self.trans = false;
\r
168 self.destroyTrans(trans, true);
\r
169 if (action === Ext.data.Api.actions.read) {
\r
170 self.onRead.call(self, action, trans, res);
\r
172 self.onWrite.call(self, action, trans, res, rs);
\r
177 * Callback for read actions
\r
178 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
179 * @param {Object} trans The request transaction object
\r
180 * @param {Object} res The server response
\r
183 onRead : function(action, trans, res) {
\r
186 result = trans.reader.readRecords(res);
\r
188 // @deprecated: fire loadexception
\r
189 this.fireEvent("loadexception", this, trans, res, e);
\r
191 this.fireEvent('exception', this, 'response', action, trans, res, e);
\r
192 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
195 if (result.success === false) {
\r
196 // @deprecated: fire old loadexception for backwards-compat.
\r
197 this.fireEvent('loadexception', this, trans, res);
\r
199 this.fireEvent('exception', this, 'remote', action, trans, res, null);
\r
201 this.fireEvent("load", this, res, trans.arg);
\r
203 trans.callback.call(trans.scope||window, result, trans.arg, result.success);
\r
206 * Callback for write actions
\r
207 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
208 * @param {Object} trans The request transaction object
\r
209 * @param {Object} res The server response
\r
212 onWrite : function(action, trans, response, rs) {
\r
213 var reader = trans.reader;
\r
215 // though we already have a response object here in STP, run through readResponse to catch any meta-data exceptions.
\r
216 var res = reader.readResponse(action, response);
\r
218 this.fireEvent('exception', this, 'response', action, trans, res, e);
\r
219 trans.callback.call(trans.scope||window, null, res, false);
\r
222 if(!res.success === true){
\r
223 this.fireEvent('exception', this, 'remote', action, trans, res, rs);
\r
224 trans.callback.call(trans.scope||window, null, res, false);
\r
227 this.fireEvent("write", this, action, res.data, res, rs, trans.arg );
\r
228 trans.callback.call(trans.scope||window, res.data, res, true);
\r
232 isLoading : function(){
\r
233 return this.trans ? true : false;
\r
236 <div id="method-Ext.data.ScriptTagProxy-abort"></div>/**
\r
237 * Abort the current server request.
\r
239 abort : function(){
\r
240 if(this.isLoading()){
\r
241 this.destroyTrans(this.trans);
\r
246 destroyTrans : function(trans, isLoaded){
\r
247 this.head.removeChild(document.getElementById(trans.scriptId));
\r
248 clearTimeout(trans.timeoutId);
\r
250 window[trans.cb] = undefined;
\r
252 delete window[trans.cb];
\r
255 // if hasn't been loaded, wait for load to remove it to prevent script error
\r
256 window[trans.cb] = function(){
\r
257 window[trans.cb] = undefined;
\r
259 delete window[trans.cb];
\r
266 handleFailure : function(trans){
\r
267 this.trans = false;
\r
268 this.destroyTrans(trans, false);
\r
269 if (trans.action === Ext.data.Api.actions.read) {
\r
270 // @deprecated firing loadexception
\r
271 this.fireEvent("loadexception", this, null, trans.arg);
\r
274 this.fireEvent('exception', this, 'response', trans.action, {
\r
278 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
282 destroy: function(){
\r
284 Ext.data.ScriptTagProxy.superclass.destroy.call(this);
\r