PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/ExtJS/docs/source/ScriptTagProxy.html

https://gitlab.com/rsilveira1987/Expresso
HTML | 309 lines | 287 code | 22 blank | 0 comment | 0 complexity | ae515d9374d9e7ccca75bd4af45b4112 MD5 | raw file
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>The source code</title>
  5. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  6. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  7. </head>
  8. <body onload="prettyPrint();">
  9. <pre class="prettyprint lang-js"><div id="cls-Ext.data.ScriptTagProxy"></div>/**
  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 <b>must</b> be executable JavaScript
  19. * source code because it is used as the source inside a &lt;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. * <p>Below is a PHP example to do the same thing:</p><pre><code>
  45. $callback = $_REQUEST['callback'];
  46. // Create the output object.
  47. $output = array('a' => 'Apple', 'b' => 'Banana');
  48. //start output
  49. if ($callback) {
  50. header('Content-Type: text/javascript');
  51. echo $callback . '(' . json_encode($output) . ');';
  52. } else {
  53. header('Content-Type: application/x-json');
  54. echo json_encode($output);
  55. }
  56. </code></pre>
  57. * <p>Below is the ASP.Net code to do the same thing:</p><pre><code>
  58. String jsonString = "{success: true}";
  59. String cb = Request.Params.Get("callback");
  60. String responseString = "";
  61. if (!String.IsNullOrEmpty(cb)) {
  62. responseString = cb + "(" + jsonString + ")";
  63. } else {
  64. responseString = jsonString;
  65. }
  66. Response.Write(responseString);
  67. </code></pre>
  68. *
  69. * @constructor
  70. * @param {Object} config A configuration object.
  71. */
  72. Ext.data.ScriptTagProxy = function(config){
  73. Ext.apply(this, config);
  74. Ext.data.ScriptTagProxy.superclass.constructor.call(this, config);
  75. this.head = document.getElementsByTagName("head")[0];
  76. <div id="event-Ext.data.ScriptTagProxy-loadexception"></div>/**
  77. * @event loadexception
  78. * <b>Deprecated</b> in favor of 'exception' event.
  79. * Fires if an exception occurs in the Proxy during data loading. This event can be fired for one of two reasons:
  80. * <ul><li><b>The load call timed out.</b> This means the load callback did not execute within the time limit
  81. * specified by {@link #timeout}. In this case, this event will be raised and the
  82. * fourth parameter (read error) will be null.</li>
  83. * <li><b>The load succeeded but the reader could not read the response.</b> This means the server returned
  84. * data, but the configured Reader threw an error while reading the data. In this case, this event will be
  85. * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>
  86. * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly
  87. * on any Store instance.
  88. * @param {Object} this
  89. * @param {Object} options The loading options that were specified (see {@link #load} for details). If the load
  90. * call timed out, this parameter will be null.
  91. * @param {Object} arg The callback's arg object passed to the {@link #load} function
  92. * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.
  93. * If the remote request returns success: false, this parameter will be null.
  94. */
  95. };
  96. Ext.data.ScriptTagProxy.TRANS_ID = 1000;
  97. Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {
  98. <div id="cfg-Ext.data.ScriptTagProxy-url"></div>/**
  99. * @cfg {String} url The URL from which to request the data object.
  100. */
  101. <div id="cfg-Ext.data.ScriptTagProxy-timeout"></div>/**
  102. * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
  103. */
  104. timeout : 30000,
  105. <div id="cfg-Ext.data.ScriptTagProxy-callbackParam"></div>/**
  106. * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
  107. * the server the name of the callback function set up by the load call to process the returned data object.
  108. * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
  109. * javascript output which calls this named function passing the data object as its only parameter.
  110. */
  111. callbackParam : "callback",
  112. <div id="cfg-Ext.data.ScriptTagProxy-nocache"></div>/**
  113. * @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter
  114. * name to the request.
  115. */
  116. nocache : true,
  117. <div id="method-Ext.data.ScriptTagProxy-doRequest"></div>/**
  118. * HttpProxy implementation of DataProxy#doRequest
  119. * @param {String} action
  120. * @param {Ext.data.Record/Ext.data.Record[]} rs If action is <tt>read</tt>, rs will be null
  121. * @param {Object} params An object containing properties which are to be used as HTTP parameters
  122. * for the request to the remote server.
  123. * @param {Ext.data.DataReader} reader The Reader object which converts the data
  124. * object into a block of Ext.data.Records.
  125. * @param {Function} callback The function into which to pass the block of Ext.data.Records.
  126. * The function must be passed <ul>
  127. * <li>The Record block object</li>
  128. * <li>The "arg" argument from the load function</li>
  129. * <li>A boolean success indicator</li>
  130. * </ul>
  131. * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
  132. * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  133. */
  134. doRequest : function(action, rs, params, reader, callback, scope, arg) {
  135. var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
  136. var url = this.buildUrl(action, rs);
  137. if (!url) {
  138. throw new Ext.data.Api.Error('invalid-url', url);
  139. }
  140. url = Ext.urlAppend(url, p);
  141. if(this.nocache){
  142. url = Ext.urlAppend(url, '_dc=' + (new Date().getTime()));
  143. }
  144. var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
  145. var trans = {
  146. id : transId,
  147. action: action,
  148. cb : "stcCallback"+transId,
  149. scriptId : "stcScript"+transId,
  150. params : params,
  151. arg : arg,
  152. url : url,
  153. callback : callback,
  154. scope : scope,
  155. reader : reader
  156. };
  157. window[trans.cb] = this.createCallback(action, rs, trans);
  158. url += String.format("&{0}={1}", this.callbackParam, trans.cb);
  159. if(this.autoAbort !== false){
  160. this.abort();
  161. }
  162. trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
  163. var script = document.createElement("script");
  164. script.setAttribute("src", url);
  165. script.setAttribute("type", "text/javascript");
  166. script.setAttribute("id", trans.scriptId);
  167. this.head.appendChild(script);
  168. this.trans = trans;
  169. },
  170. // @private createCallback
  171. createCallback : function(action, rs, trans) {
  172. var self = this;
  173. return function(res) {
  174. self.trans = false;
  175. self.destroyTrans(trans, true);
  176. if (action === Ext.data.Api.actions.read) {
  177. self.onRead.call(self, action, trans, res);
  178. } else {
  179. self.onWrite.call(self, action, trans, res, rs);
  180. }
  181. };
  182. },
  183. <div id="method-Ext.data.ScriptTagProxy-onRead"></div>/**
  184. * Callback for read actions
  185. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  186. * @param {Object} trans The request transaction object
  187. * @param {Object} res The server response
  188. * @protected
  189. */
  190. onRead : function(action, trans, res) {
  191. var result;
  192. try {
  193. result = trans.reader.readRecords(res);
  194. }catch(e){
  195. // @deprecated: fire loadexception
  196. this.fireEvent("loadexception", this, trans, res, e);
  197. this.fireEvent('exception', this, 'response', action, trans, res, e);
  198. trans.callback.call(trans.scope||window, null, trans.arg, false);
  199. return;
  200. }
  201. if (result.success === false) {
  202. // @deprecated: fire old loadexception for backwards-compat.
  203. this.fireEvent('loadexception', this, trans, res);
  204. this.fireEvent('exception', this, 'remote', action, trans, res, null);
  205. } else {
  206. this.fireEvent("load", this, res, trans.arg);
  207. }
  208. trans.callback.call(trans.scope||window, result, trans.arg, result.success);
  209. },
  210. <div id="method-Ext.data.ScriptTagProxy-onWrite"></div>/**
  211. * Callback for write actions
  212. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  213. * @param {Object} trans The request transaction object
  214. * @param {Object} res The server response
  215. * @protected
  216. */
  217. onWrite : function(action, trans, response, rs) {
  218. var reader = trans.reader;
  219. try {
  220. // though we already have a response object here in STP, run through readResponse to catch any meta-data exceptions.
  221. var res = reader.readResponse(action, response);
  222. } catch (e) {
  223. this.fireEvent('exception', this, 'response', action, trans, res, e);
  224. trans.callback.call(trans.scope||window, null, res, false);
  225. return;
  226. }
  227. if(!res.success === true){
  228. this.fireEvent('exception', this, 'remote', action, trans, res, rs);
  229. trans.callback.call(trans.scope||window, null, res, false);
  230. return;
  231. }
  232. this.fireEvent("write", this, action, res.data, res, rs, trans.arg );
  233. trans.callback.call(trans.scope||window, res.data, res, true);
  234. },
  235. // private
  236. isLoading : function(){
  237. return this.trans ? true : false;
  238. },
  239. <div id="method-Ext.data.ScriptTagProxy-abort"></div>/**
  240. * Abort the current server request.
  241. */
  242. abort : function(){
  243. if(this.isLoading()){
  244. this.destroyTrans(this.trans);
  245. }
  246. },
  247. // private
  248. destroyTrans : function(trans, isLoaded){
  249. this.head.removeChild(document.getElementById(trans.scriptId));
  250. clearTimeout(trans.timeoutId);
  251. if(isLoaded){
  252. window[trans.cb] = undefined;
  253. try{
  254. delete window[trans.cb];
  255. }catch(e){}
  256. }else{
  257. // if hasn't been loaded, wait for load to remove it to prevent script error
  258. window[trans.cb] = function(){
  259. window[trans.cb] = undefined;
  260. try{
  261. delete window[trans.cb];
  262. }catch(e){}
  263. };
  264. }
  265. },
  266. // private
  267. handleFailure : function(trans){
  268. this.trans = false;
  269. this.destroyTrans(trans, false);
  270. if (trans.action === Ext.data.Api.actions.read) {
  271. // @deprecated firing loadexception
  272. this.fireEvent("loadexception", this, null, trans.arg);
  273. }
  274. this.fireEvent('exception', this, 'response', trans.action, {
  275. response: null,
  276. options: trans.arg
  277. });
  278. trans.callback.call(trans.scope||window, null, trans.arg, false);
  279. },
  280. // inherit docs
  281. destroy: function(){
  282. this.abort();
  283. Ext.data.ScriptTagProxy.superclass.destroy.call(this);
  284. }
  285. });</pre>
  286. </body>
  287. </html>