27 |
jpm |
1 |
/*
|
|
|
2 |
* Ext JS Library 2.0.2
|
|
|
3 |
* Copyright(c) 2006-2008, Ext JS, LLC.
|
|
|
4 |
* licensing@extjs.com
|
|
|
5 |
*
|
|
|
6 |
* http://extjs.com/license
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @class Ext.data.ScriptTagProxy
|
|
|
11 |
* @extends Ext.data.DataProxy
|
|
|
12 |
* An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain
|
|
|
13 |
* other than the originating domain of the running page.<br>
|
|
|
14 |
* <p>
|
|
|
15 |
* <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
|
|
|
16 |
* of the running page, you must use this class, rather than HttpProxy.</b><br>
|
|
|
17 |
* <p>
|
|
|
18 |
* The content passed back from a server resource requested by a ScriptTagProxy is executable JavaScript
|
|
|
19 |
* source code that is used as the source inside a <script> tag.<br>
|
|
|
20 |
* <p>
|
|
|
21 |
* In order for the browser to process the returned data, the server must wrap the data object
|
|
|
22 |
* with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.
|
|
|
23 |
* Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy
|
|
|
24 |
* depending on whether the callback name was passed:
|
|
|
25 |
* <p>
|
|
|
26 |
* <pre><code>
|
|
|
27 |
boolean scriptTag = false;
|
|
|
28 |
String cb = request.getParameter("callback");
|
|
|
29 |
if (cb != null) {
|
|
|
30 |
scriptTag = true;
|
|
|
31 |
response.setContentType("text/javascript");
|
|
|
32 |
} else {
|
|
|
33 |
response.setContentType("application/x-json");
|
|
|
34 |
}
|
|
|
35 |
Writer out = response.getWriter();
|
|
|
36 |
if (scriptTag) {
|
|
|
37 |
out.write(cb + "(");
|
|
|
38 |
}
|
|
|
39 |
out.print(dataBlock.toJsonString());
|
|
|
40 |
if (scriptTag) {
|
|
|
41 |
out.write(");");
|
|
|
42 |
}
|
|
|
43 |
</code></pre>
|
|
|
44 |
*
|
|
|
45 |
* @constructor
|
|
|
46 |
* @param {Object} config A configuration object.
|
|
|
47 |
*/
|
|
|
48 |
Ext.data.ScriptTagProxy = function(config){
|
|
|
49 |
Ext.data.ScriptTagProxy.superclass.constructor.call(this);
|
|
|
50 |
Ext.apply(this, config);
|
|
|
51 |
this.head = document.getElementsByTagName("head")[0];
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
Ext.data.ScriptTagProxy.TRANS_ID = 1000;
|
|
|
55 |
|
|
|
56 |
Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {
|
|
|
57 |
/**
|
|
|
58 |
* @cfg {String} url The URL from which to request the data object.
|
|
|
59 |
*/
|
|
|
60 |
/**
|
|
|
61 |
* @cfg {Number} timeout (Optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
|
|
|
62 |
*/
|
|
|
63 |
timeout : 30000,
|
|
|
64 |
/**
|
|
|
65 |
* @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
|
|
|
66 |
* the server the name of the callback function set up by the load call to process the returned data object.
|
|
|
67 |
* Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
|
|
|
68 |
* javascript output which calls this named function passing the data object as its only parameter.
|
|
|
69 |
*/
|
|
|
70 |
callbackParam : "callback",
|
|
|
71 |
/**
|
|
|
72 |
* @cfg {Boolean} nocache (Optional) Defaults to true. Disable cacheing by adding a unique parameter
|
|
|
73 |
* name to the request.
|
|
|
74 |
*/
|
|
|
75 |
nocache : true,
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Load data from the configured URL, read the data object into
|
|
|
79 |
* a block of Ext.data.Records using the passed Ext.data.DataReader implementation, and
|
|
|
80 |
* process that block using the passed callback.
|
|
|
81 |
* @param {Object} params An object containing properties which are to be used as HTTP parameters
|
|
|
82 |
* for the request to the remote server.
|
|
|
83 |
* @param {Ext.data.DataReader} reader The Reader object which converts the data
|
|
|
84 |
* object into a block of Ext.data.Records.
|
|
|
85 |
* @param {Function} callback The function into which to pass the block of Ext.data.Records.
|
|
|
86 |
* The function must be passed <ul>
|
|
|
87 |
* <li>The Record block object</li>
|
|
|
88 |
* <li>The "arg" argument from the load function</li>
|
|
|
89 |
* <li>A boolean success indicator</li>
|
|
|
90 |
* </ul>
|
|
|
91 |
* @param {Object} scope The scope in which to call the callback
|
|
|
92 |
* @param {Object} arg An optional argument which is passed to the callback as its second parameter.
|
|
|
93 |
*/
|
|
|
94 |
load : function(params, reader, callback, scope, arg){
|
|
|
95 |
if(this.fireEvent("beforeload", this, params) !== false){
|
|
|
96 |
|
|
|
97 |
var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
|
|
|
98 |
|
|
|
99 |
var url = this.url;
|
|
|
100 |
url += (url.indexOf("?") != -1 ? "&" : "?") + p;
|
|
|
101 |
if(this.nocache){
|
|
|
102 |
url += "&_dc=" + (new Date().getTime());
|
|
|
103 |
}
|
|
|
104 |
var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
|
|
|
105 |
var trans = {
|
|
|
106 |
id : transId,
|
|
|
107 |
cb : "stcCallback"+transId,
|
|
|
108 |
scriptId : "stcScript"+transId,
|
|
|
109 |
params : params,
|
|
|
110 |
arg : arg,
|
|
|
111 |
url : url,
|
|
|
112 |
callback : callback,
|
|
|
113 |
scope : scope,
|
|
|
114 |
reader : reader
|
|
|
115 |
};
|
|
|
116 |
var conn = this;
|
|
|
117 |
|
|
|
118 |
window[trans.cb] = function(o){
|
|
|
119 |
conn.handleResponse(o, trans);
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
url += String.format("&{0}={1}", this.callbackParam, trans.cb);
|
|
|
123 |
|
|
|
124 |
if(this.autoAbort !== false){
|
|
|
125 |
this.abort();
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
|
|
|
129 |
|
|
|
130 |
var script = document.createElement("script");
|
|
|
131 |
script.setAttribute("src", url);
|
|
|
132 |
script.setAttribute("type", "text/javascript");
|
|
|
133 |
script.setAttribute("id", trans.scriptId);
|
|
|
134 |
this.head.appendChild(script);
|
|
|
135 |
|
|
|
136 |
this.trans = trans;
|
|
|
137 |
}else{
|
|
|
138 |
callback.call(scope||this, null, arg, false);
|
|
|
139 |
}
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
// private
|
|
|
143 |
isLoading : function(){
|
|
|
144 |
return this.trans ? true : false;
|
|
|
145 |
},
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Abort the current server request.
|
|
|
149 |
*/
|
|
|
150 |
abort : function(){
|
|
|
151 |
if(this.isLoading()){
|
|
|
152 |
this.destroyTrans(this.trans);
|
|
|
153 |
}
|
|
|
154 |
},
|
|
|
155 |
|
|
|
156 |
// private
|
|
|
157 |
destroyTrans : function(trans, isLoaded){
|
|
|
158 |
this.head.removeChild(document.getElementById(trans.scriptId));
|
|
|
159 |
clearTimeout(trans.timeoutId);
|
|
|
160 |
if(isLoaded){
|
|
|
161 |
window[trans.cb] = undefined;
|
|
|
162 |
try{
|
|
|
163 |
delete window[trans.cb];
|
|
|
164 |
}catch(e){}
|
|
|
165 |
}else{
|
|
|
166 |
// if hasn't been loaded, wait for load to remove it to prevent script error
|
|
|
167 |
window[trans.cb] = function(){
|
|
|
168 |
window[trans.cb] = undefined;
|
|
|
169 |
try{
|
|
|
170 |
delete window[trans.cb];
|
|
|
171 |
}catch(e){}
|
|
|
172 |
};
|
|
|
173 |
}
|
|
|
174 |
},
|
|
|
175 |
|
|
|
176 |
// private
|
|
|
177 |
handleResponse : function(o, trans){
|
|
|
178 |
this.trans = false;
|
|
|
179 |
this.destroyTrans(trans, true);
|
|
|
180 |
var result;
|
|
|
181 |
try {
|
|
|
182 |
result = trans.reader.readRecords(o);
|
|
|
183 |
}catch(e){
|
|
|
184 |
this.fireEvent("loadexception", this, o, trans.arg, e);
|
|
|
185 |
trans.callback.call(trans.scope||window, null, trans.arg, false);
|
|
|
186 |
return;
|
|
|
187 |
}
|
|
|
188 |
this.fireEvent("load", this, o, trans.arg);
|
|
|
189 |
trans.callback.call(trans.scope||window, result, trans.arg, true);
|
|
|
190 |
},
|
|
|
191 |
|
|
|
192 |
// private
|
|
|
193 |
handleFailure : function(trans){
|
|
|
194 |
this.trans = false;
|
|
|
195 |
this.destroyTrans(trans, false);
|
|
|
196 |
this.fireEvent("loadexception", this, null, trans.arg);
|
|
|
197 |
trans.callback.call(trans.scope||window, null, trans.arg, false);
|
|
|
198 |
}
|
|
|
199 |
});
|