You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
12 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  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; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-HttpProxy-method-constructor'><span id='Ext-data-HttpProxy'>/**
  19. </span></span> * @class Ext.data.HttpProxy
  20. * @extends Ext.data.DataProxy
  21. * &lt;p&gt;An implementation of {@link Ext.data.DataProxy} that processes data requests within the same
  22. * domain of the originating page.&lt;/p&gt;
  23. * &lt;p&gt;&lt;b&gt;Note&lt;/b&gt;: this class cannot be used to retrieve data from a domain other
  24. * than the domain from which the running page was served. For cross-domain requests, use a
  25. * {@link Ext.data.ScriptTagProxy ScriptTagProxy}.&lt;/p&gt;
  26. * &lt;p&gt;Be aware that to enable the browser to parse an XML document, the server must set
  27. * the Content-Type header in the HTTP response to &quot;&lt;tt&gt;text/xml&lt;/tt&gt;&quot;.&lt;/p&gt;
  28. * @constructor
  29. * @param {Object} conn
  30. * An {@link Ext.data.Connection} object, or options parameter to {@link Ext.Ajax#request}.
  31. * &lt;p&gt;Note that if this HttpProxy is being used by a {@link Ext.data.Store Store}, then the
  32. * Store's call to {@link #load} will override any specified &lt;tt&gt;callback&lt;/tt&gt; and &lt;tt&gt;params&lt;/tt&gt;
  33. * options. In this case, use the Store's {@link Ext.data.Store#events events} to modify parameters,
  34. * or react to loading events. The Store's {@link Ext.data.Store#baseParams baseParams} may also be
  35. * used to pass parameters known at instantiation time.&lt;/p&gt;
  36. * &lt;p&gt;If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make
  37. * the request.&lt;/p&gt;
  38. */
  39. Ext.data.HttpProxy = function(conn){
  40. Ext.data.HttpProxy.superclass.constructor.call(this, conn);
  41. <span id='Ext-data-HttpProxy-property-conn'> /**
  42. </span> * The Connection object (Or options parameter to {@link Ext.Ajax#request}) which this HttpProxy
  43. * uses to make requests to the server. Properties of this object may be changed dynamically to
  44. * change the way data is requested.
  45. * @property
  46. */
  47. this.conn = conn;
  48. // nullify the connection url. The url param has been copied to 'this' above. The connection
  49. // url will be set during each execution of doRequest when buildUrl is called. This makes it easier for users to override the
  50. // connection url during beforeaction events (ie: beforeload, beforewrite, etc).
  51. // Url is always re-defined during doRequest.
  52. this.conn.url = null;
  53. this.useAjax = !conn || !conn.events;
  54. // A hash containing active requests, keyed on action [Ext.data.Api.actions.create|read|update|destroy]
  55. var actions = Ext.data.Api.actions;
  56. this.activeRequest = {};
  57. for (var verb in actions) {
  58. this.activeRequest[actions[verb]] = undefined;
  59. }
  60. };
  61. Ext.extend(Ext.data.HttpProxy, Ext.data.DataProxy, {
  62. <span id='Ext-data-HttpProxy-method-getConnection'> /**
  63. </span> * Return the {@link Ext.data.Connection} object being used by this Proxy.
  64. * @return {Connection} The Connection object. This object may be used to subscribe to events on
  65. * a finer-grained basis than the DataProxy events.
  66. */
  67. getConnection : function() {
  68. return this.useAjax ? Ext.Ajax : this.conn;
  69. },
  70. <span id='Ext-data-HttpProxy-method-setUrl'> /**
  71. </span> * Used for overriding the url used for a single request. Designed to be called during a beforeaction event. Calling setUrl
  72. * will override any urls set via the api configuration parameter. Set the optional parameter makePermanent to set the url for
  73. * all subsequent requests. If not set to makePermanent, the next request will use the same url or api configuration defined
  74. * in the initial proxy configuration.
  75. * @param {String} url
  76. * @param {Boolean} makePermanent (Optional) [false]
  77. *
  78. * (e.g.: beforeload, beforesave, etc).
  79. */
  80. setUrl : function(url, makePermanent) {
  81. this.conn.url = url;
  82. if (makePermanent === true) {
  83. this.url = url;
  84. this.api = null;
  85. Ext.data.Api.prepare(this);
  86. }
  87. },
  88. <span id='Ext-data-HttpProxy-method-doRequest'> /**
  89. </span> * HttpProxy implementation of DataProxy#doRequest
  90. * @param {String} action The crud action type (create, read, update, destroy)
  91. * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
  92. * @param {Object} params An object containing properties which are to be used as HTTP parameters
  93. * for the request to the remote server.
  94. * @param {Ext.data.DataReader} reader The Reader object which converts the data
  95. * object into a block of Ext.data.Records.
  96. * @param {Function} callback
  97. * &lt;div class=&quot;sub-desc&quot;&gt;&lt;p&gt;A function to be called after the request.
  98. * The &lt;tt&gt;callback&lt;/tt&gt; is passed the following arguments:&lt;ul&gt;
  99. * &lt;li&gt;&lt;tt&gt;r&lt;/tt&gt; : Ext.data.Record[] The block of Ext.data.Records.&lt;/li&gt;
  100. * &lt;li&gt;&lt;tt&gt;options&lt;/tt&gt;: Options object from the action request&lt;/li&gt;
  101. * &lt;li&gt;&lt;tt&gt;success&lt;/tt&gt;: Boolean success indicator&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;/div&gt;
  102. * @param {Object} scope The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the callback function is executed. Defaults to the browser window.
  103. * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  104. * @protected
  105. */
  106. doRequest : function(action, rs, params, reader, cb, scope, arg) {
  107. var o = {
  108. method: (this.api[action]) ? this.api[action]['method'] : undefined,
  109. request: {
  110. callback : cb,
  111. scope : scope,
  112. arg : arg
  113. },
  114. reader: reader,
  115. callback : this.createCallback(action, rs),
  116. scope: this
  117. };
  118. // If possible, transmit data using jsonData || xmlData on Ext.Ajax.request (An installed DataWriter would have written it there.).
  119. // Use std HTTP params otherwise.
  120. if (params.jsonData) {
  121. o.jsonData = params.jsonData;
  122. } else if (params.xmlData) {
  123. o.xmlData = params.xmlData;
  124. } else {
  125. o.params = params || {};
  126. }
  127. // Set the connection url. If this.conn.url is not null here,
  128. // the user must have overridden the url during a beforewrite/beforeload event-handler.
  129. // this.conn.url is nullified after each request.
  130. this.conn.url = this.buildUrl(action, rs);
  131. if(this.useAjax){
  132. Ext.applyIf(o, this.conn);
  133. // If a currently running read request is found, abort it
  134. if (action == Ext.data.Api.actions.read &amp;&amp; this.activeRequest[action]) {
  135. Ext.Ajax.abort(this.activeRequest[action]);
  136. }
  137. this.activeRequest[action] = Ext.Ajax.request(o);
  138. }else{
  139. this.conn.request(o);
  140. }
  141. // request is sent, nullify the connection url in preparation for the next request
  142. this.conn.url = null;
  143. },
  144. <span id='Ext-data-HttpProxy-method-createCallback'> /**
  145. </span> * Returns a callback function for a request. Note a special case is made for the
  146. * read action vs all the others.
  147. * @param {String} action [create|update|delete|load]
  148. * @param {Ext.data.Record[]} rs The Store-recordset being acted upon
  149. * @private
  150. */
  151. createCallback : function(action, rs) {
  152. return function(o, success, response) {
  153. this.activeRequest[action] = undefined;
  154. if (!success) {
  155. if (action === Ext.data.Api.actions.read) {
  156. // @deprecated: fire loadexception for backwards compat.
  157. // TODO remove
  158. this.fireEvent('loadexception', this, o, response);
  159. }
  160. this.fireEvent('exception', this, 'response', action, o, response);
  161. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  162. return;
  163. }
  164. if (action === Ext.data.Api.actions.read) {
  165. this.onRead(action, o, response);
  166. } else {
  167. this.onWrite(action, o, response, rs);
  168. }
  169. };
  170. },
  171. <span id='Ext-data-HttpProxy-method-onRead'> /**
  172. </span> * Callback for read action
  173. * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
  174. * @param {Object} o The request transaction object
  175. * @param {Object} res The server response
  176. * @fires loadexception (deprecated)
  177. * @fires exception
  178. * @fires load
  179. * @protected
  180. */
  181. onRead : function(action, o, response) {
  182. var result;
  183. try {
  184. result = o.reader.read(response);
  185. }catch(e){
  186. // @deprecated: fire old loadexception for backwards-compat.
  187. // TODO remove
  188. this.fireEvent('loadexception', this, o, response, e);
  189. this.fireEvent('exception', this, 'response', action, o, response, e);
  190. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  191. return;
  192. }
  193. if (result.success === false) {
  194. // @deprecated: fire old loadexception for backwards-compat.
  195. // TODO remove
  196. this.fireEvent('loadexception', this, o, response);
  197. // Get DataReader read-back a response-object to pass along to exception event
  198. var res = o.reader.readResponse(action, response);
  199. this.fireEvent('exception', this, 'remote', action, o, res, null);
  200. }
  201. else {
  202. this.fireEvent('load', this, o, o.request.arg);
  203. }
  204. // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance
  205. // the calls to request.callback(...) in each will have to be made identical.
  206. // NOTE reader.readResponse does not currently return Ext.data.Response
  207. o.request.callback.call(o.request.scope, result, o.request.arg, result.success);
  208. },
  209. <span id='Ext-data-HttpProxy-method-onWrite'> /**
  210. </span> * Callback for write actions
  211. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  212. * @param {Object} trans The request transaction object
  213. * @param {Object} res The server response
  214. * @fires exception
  215. * @fires write
  216. * @protected
  217. */
  218. onWrite : function(action, o, response, rs) {
  219. var reader = o.reader;
  220. var res;
  221. try {
  222. res = reader.readResponse(action, response);
  223. } catch (e) {
  224. this.fireEvent('exception', this, 'response', action, o, response, e);
  225. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  226. return;
  227. }
  228. if (res.success === true) {
  229. this.fireEvent('write', this, action, res.data, res, rs, o.request.arg);
  230. } else {
  231. this.fireEvent('exception', this, 'remote', action, o, res, rs);
  232. }
  233. // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance
  234. // the calls to request.callback(...) in each will have to be made similar.
  235. // NOTE reader.readResponse does not currently return Ext.data.Response
  236. o.request.callback.call(o.request.scope, res.data, res, res.success);
  237. },
  238. // inherit docs
  239. destroy: function(){
  240. if(!this.useAjax){
  241. this.conn.abort();
  242. }else if(this.activeRequest){
  243. var actions = Ext.data.Api.actions;
  244. for (var verb in actions) {
  245. if(this.activeRequest[actions[verb]]){
  246. Ext.Ajax.abort(this.activeRequest[actions[verb]]);
  247. }
  248. }
  249. }
  250. Ext.data.HttpProxy.superclass.destroy.call(this);
  251. }
  252. });</pre>
  253. </body>
  254. </html>