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.

525 lines
28 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-DataProxy'>/**
  19. </span> * @class Ext.data.DataProxy
  20. * @extends Ext.util.Observable
  21. * &lt;p&gt;Abstract base class for implementations which provide retrieval of unformatted data objects.
  22. * This class is intended to be extended and should not be created directly. For existing implementations,
  23. * see {@link Ext.data.DirectProxy}, {@link Ext.data.HttpProxy}, {@link Ext.data.ScriptTagProxy} and
  24. * {@link Ext.data.MemoryProxy}.&lt;/p&gt;
  25. * &lt;p&gt;DataProxy implementations are usually used in conjunction with an implementation of {@link Ext.data.DataReader}
  26. * (of the appropriate type which knows how to parse the data object) to provide a block of
  27. * {@link Ext.data.Records} to an {@link Ext.data.Store}.&lt;/p&gt;
  28. * &lt;p&gt;The parameter to a DataProxy constructor may be an {@link Ext.data.Connection} or can also be the
  29. * config object to an {@link Ext.data.Connection}.&lt;/p&gt;
  30. * &lt;p&gt;Custom implementations must implement either the &lt;code&gt;&lt;b&gt;doRequest&lt;/b&gt;&lt;/code&gt; method (preferred) or the
  31. * &lt;code&gt;load&lt;/code&gt; method (deprecated). See
  32. * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#doRequest doRequest} or
  33. * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#load load} for additional details.&lt;/p&gt;
  34. * &lt;p&gt;&lt;b&gt;&lt;u&gt;Example 1&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
  35. * &lt;pre&gt;&lt;code&gt;
  36. proxy: new Ext.data.ScriptTagProxy({
  37. {@link Ext.data.Connection#url url}: 'http://extjs.com/forum/topics-remote.php'
  38. }),
  39. * &lt;/code&gt;&lt;/pre&gt;
  40. * &lt;p&gt;&lt;b&gt;&lt;u&gt;Example 2&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
  41. * &lt;pre&gt;&lt;code&gt;
  42. proxy : new Ext.data.HttpProxy({
  43. {@link Ext.data.Connection#method method}: 'GET',
  44. {@link Ext.data.HttpProxy#prettyUrls prettyUrls}: false,
  45. {@link Ext.data.Connection#url url}: 'local/default.php', // see options parameter for {@link Ext.Ajax#request}
  46. {@link #api}: {
  47. // all actions except the following will use above url
  48. create : 'local/new.php',
  49. update : 'local/update.php'
  50. }
  51. }),
  52. * &lt;/code&gt;&lt;/pre&gt;
  53. * &lt;p&gt;And &lt;b&gt;new in Ext version 3&lt;/b&gt;, attach centralized event-listeners upon the DataProxy class itself! This is a great place
  54. * to implement a &lt;i&gt;messaging system&lt;/i&gt; to centralize your application's user-feedback and error-handling.&lt;/p&gt;
  55. * &lt;pre&gt;&lt;code&gt;
  56. // Listen to all &quot;beforewrite&quot; event fired by all proxies.
  57. Ext.data.DataProxy.on('beforewrite', function(proxy, action) {
  58. console.log('beforewrite: ', action);
  59. });
  60. // Listen to &quot;write&quot; event fired by all proxies
  61. Ext.data.DataProxy.on('write', function(proxy, action, data, res, rs) {
  62. console.info('write: ', action);
  63. });
  64. // Listen to &quot;exception&quot; event fired by all proxies
  65. Ext.data.DataProxy.on('exception', function(proxy, type, action, exception) {
  66. console.error(type + action + ' exception);
  67. });
  68. * &lt;/code&gt;&lt;/pre&gt;
  69. * &lt;b&gt;Note:&lt;/b&gt; These three events are all fired with the signature of the corresponding &lt;i&gt;DataProxy instance&lt;/i&gt; event {@link #beforewrite beforewrite}, {@link #write write} and {@link #exception exception}.
  70. */
  71. Ext.data.DataProxy = function(conn){
  72. // make sure we have a config object here to support ux proxies.
  73. // All proxies should now send config into superclass constructor.
  74. conn = conn || {};
  75. // This line caused a bug when people use custom Connection object having its own request method.
  76. // http://extjs.com/forum/showthread.php?t=67194. Have to set DataProxy config
  77. //Ext.applyIf(this, conn);
  78. this.api = conn.api;
  79. this.url = conn.url;
  80. this.restful = conn.restful;
  81. this.listeners = conn.listeners;
  82. // deprecated
  83. this.prettyUrls = conn.prettyUrls;
  84. <span id='Ext-data-DataProxy-cfg-api'> /**
  85. </span> * @cfg {Object} api
  86. * Specific urls to call on CRUD action methods &quot;read&quot;, &quot;create&quot;, &quot;update&quot; and &quot;destroy&quot;.
  87. * Defaults to:&lt;pre&gt;&lt;code&gt;
  88. api: {
  89. read : undefined,
  90. create : undefined,
  91. update : undefined,
  92. destroy : undefined
  93. }
  94. * &lt;/code&gt;&lt;/pre&gt;
  95. * &lt;p&gt;The url is built based upon the action being executed &lt;tt&gt;[load|create|save|destroy]&lt;/tt&gt;
  96. * using the commensurate &lt;tt&gt;{@link #api}&lt;/tt&gt; property, or if undefined default to the
  97. * configured {@link Ext.data.Store}.{@link Ext.data.Store#url url}.&lt;/p&gt;&lt;br&gt;
  98. * &lt;p&gt;For example:&lt;/p&gt;
  99. * &lt;pre&gt;&lt;code&gt;
  100. api: {
  101. load : '/controller/load',
  102. create : '/controller/new', // Server MUST return idProperty of new record
  103. save : '/controller/update',
  104. destroy : '/controller/destroy_action'
  105. }
  106. // Alternatively, one can use the object-form to specify each API-action
  107. api: {
  108. load: {url: 'read.php', method: 'GET'},
  109. create: 'create.php',
  110. destroy: 'destroy.php',
  111. save: 'update.php'
  112. }
  113. * &lt;/code&gt;&lt;/pre&gt;
  114. * &lt;p&gt;If the specific URL for a given CRUD action is undefined, the CRUD action request
  115. * will be directed to the configured &lt;tt&gt;{@link Ext.data.Connection#url url}&lt;/tt&gt;.&lt;/p&gt;
  116. * &lt;br&gt;&lt;p&gt;&lt;b&gt;Note&lt;/b&gt;: To modify the URL for an action dynamically the appropriate API
  117. * property should be modified before the action is requested using the corresponding before
  118. * action event. For example to modify the URL associated with the load action:
  119. * &lt;pre&gt;&lt;code&gt;
  120. // modify the url for the action
  121. myStore.on({
  122. beforeload: {
  123. fn: function (store, options) {
  124. // use &lt;tt&gt;{@link Ext.data.HttpProxy#setUrl setUrl}&lt;/tt&gt; to change the URL for *just* this request.
  125. store.proxy.setUrl('changed1.php');
  126. // set optional second parameter to true to make this URL change
  127. // permanent, applying this URL for all subsequent requests.
  128. store.proxy.setUrl('changed1.php', true);
  129. // Altering the proxy API should be done using the public
  130. // method &lt;tt&gt;{@link Ext.data.DataProxy#setApi setApi}&lt;/tt&gt;.
  131. store.proxy.setApi('read', 'changed2.php');
  132. // Or set the entire API with a config-object.
  133. // When using the config-object option, you must redefine the &lt;b&gt;entire&lt;/b&gt;
  134. // API -- not just a specific action of it.
  135. store.proxy.setApi({
  136. read : 'changed_read.php',
  137. create : 'changed_create.php',
  138. update : 'changed_update.php',
  139. destroy : 'changed_destroy.php'
  140. });
  141. }
  142. }
  143. });
  144. * &lt;/code&gt;&lt;/pre&gt;
  145. * &lt;/p&gt;
  146. */
  147. this.addEvents(
  148. <span id='Ext-data-DataProxy-event-exception'> /**
  149. </span> * @event exception
  150. * &lt;p&gt;Fires if an exception occurs in the Proxy during a remote request. This event is relayed
  151. * through a corresponding {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},
  152. * so any Store instance may observe this event.&lt;/p&gt;
  153. * &lt;p&gt;In addition to being fired through the DataProxy instance that raised the event, this event is also fired
  154. * through the Ext.data.DataProxy &lt;i&gt;class&lt;/i&gt; to allow for centralized processing of exception events from &lt;b&gt;all&lt;/b&gt;
  155. * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.&lt;/p&gt;
  156. * &lt;p&gt;This event can be fired for one of two reasons:&lt;/p&gt;
  157. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  158. * &lt;li&gt;remote-request &lt;b&gt;failed&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;
  159. * The server did not return status === 200.
  160. * &lt;/div&gt;&lt;/li&gt;
  161. * &lt;li&gt;remote-request &lt;b&gt;succeeded&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;
  162. * The remote-request succeeded but the reader could not read the response.
  163. * This means the server returned data, but the configured Reader threw an
  164. * error while reading the response. In this case, this event will be
  165. * raised and the caught error will be passed along into this event.
  166. * &lt;/div&gt;&lt;/li&gt;
  167. * &lt;/ul&gt;&lt;/div&gt;
  168. * &lt;br&gt;&lt;p&gt;This event fires with two different contexts based upon the 2nd
  169. * parameter &lt;tt&gt;type [remote|response]&lt;/tt&gt;. The first four parameters
  170. * are identical between the two contexts -- only the final two parameters
  171. * differ.&lt;/p&gt;
  172. * @param {DataProxy} this The proxy that sent the request
  173. * @param {String} type
  174. * &lt;p&gt;The value of this parameter will be either &lt;tt&gt;'response'&lt;/tt&gt; or &lt;tt&gt;'remote'&lt;/tt&gt;.&lt;/p&gt;
  175. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  176. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;'response'&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;
  177. * &lt;p&gt;An &lt;b&gt;invalid&lt;/b&gt; response from the server was returned: either 404,
  178. * 500 or the response meta-data does not match that defined in the DataReader
  179. * (e.g.: root, idProperty, successProperty).&lt;/p&gt;
  180. * &lt;/div&gt;&lt;/li&gt;
  181. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;'remote'&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;
  182. * &lt;p&gt;A &lt;b&gt;valid&lt;/b&gt; response was returned from the server having
  183. * successProperty === false. This response might contain an error-message
  184. * sent from the server. For example, the user may have failed
  185. * authentication/authorization or a database validation error occurred.&lt;/p&gt;
  186. * &lt;/div&gt;&lt;/li&gt;
  187. * &lt;/ul&gt;&lt;/div&gt;
  188. * @param {String} action Name of the action (see {@link Ext.data.Api#actions}.
  189. * @param {Object} options The options for the action that were specified in the {@link #request}.
  190. * @param {Object} response
  191. * &lt;p&gt;The value of this parameter depends on the value of the &lt;code&gt;type&lt;/code&gt; parameter:&lt;/p&gt;
  192. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  193. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;'response'&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;
  194. * &lt;p&gt;The raw browser response object (e.g.: XMLHttpRequest)&lt;/p&gt;
  195. * &lt;/div&gt;&lt;/li&gt;
  196. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;'remote'&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;
  197. * &lt;p&gt;The decoded response object sent from the server.&lt;/p&gt;
  198. * &lt;/div&gt;&lt;/li&gt;
  199. * &lt;/ul&gt;&lt;/div&gt;
  200. * @param {Mixed} arg
  201. * &lt;p&gt;The type and value of this parameter depends on the value of the &lt;code&gt;type&lt;/code&gt; parameter:&lt;/p&gt;
  202. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  203. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;'response'&lt;/tt&gt;&lt;/b&gt; : Error&lt;div class=&quot;sub-desc&quot;&gt;
  204. * &lt;p&gt;The JavaScript Error object caught if the configured Reader could not read the data.
  205. * If the remote request returns success===false, this parameter will be null.&lt;/p&gt;
  206. * &lt;/div&gt;&lt;/li&gt;
  207. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;'remote'&lt;/tt&gt;&lt;/b&gt; : Record/Record[]&lt;div class=&quot;sub-desc&quot;&gt;
  208. * &lt;p&gt;This parameter will only exist if the &lt;tt&gt;action&lt;/tt&gt; was a &lt;b&gt;write&lt;/b&gt; action
  209. * (Ext.data.Api.actions.create|update|destroy).&lt;/p&gt;
  210. * &lt;/div&gt;&lt;/li&gt;
  211. * &lt;/ul&gt;&lt;/div&gt;
  212. */
  213. 'exception',
  214. <span id='Ext-data-DataProxy-event-beforeload'> /**
  215. </span> * @event beforeload
  216. * Fires before a request to retrieve a data object.
  217. * @param {DataProxy} this The proxy for the request
  218. * @param {Object} params The params object passed to the {@link #request} function
  219. */
  220. 'beforeload',
  221. <span id='Ext-data-DataProxy-event-load'> /**
  222. </span> * @event load
  223. * Fires before the load method's callback is called.
  224. * @param {DataProxy} this The proxy for the request
  225. * @param {Object} o The request transaction object
  226. * @param {Object} options The callback's &lt;tt&gt;options&lt;/tt&gt; property as passed to the {@link #request} function
  227. */
  228. 'load',
  229. <span id='Ext-data-DataProxy-event-loadexception'> /**
  230. </span> * @event loadexception
  231. * &lt;p&gt;This event is &lt;b&gt;deprecated&lt;/b&gt;. The signature of the loadexception event
  232. * varies depending on the proxy, use the catch-all {@link #exception} event instead.
  233. * This event will fire in addition to the {@link #exception} event.&lt;/p&gt;
  234. * @param {misc} misc See {@link #exception}.
  235. * @deprecated
  236. */
  237. 'loadexception',
  238. <span id='Ext-data-DataProxy-event-beforewrite'> /**
  239. </span> * @event beforewrite
  240. * &lt;p&gt;Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy&lt;/p&gt;
  241. * &lt;p&gt;In addition to being fired through the DataProxy instance that raised the event, this event is also fired
  242. * through the Ext.data.DataProxy &lt;i&gt;class&lt;/i&gt; to allow for centralized processing of beforewrite events from &lt;b&gt;all&lt;/b&gt;
  243. * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.&lt;/p&gt;
  244. * @param {DataProxy} this The proxy for the request
  245. * @param {String} action [Ext.data.Api.actions.create|update|destroy]
  246. * @param {Record/Record[]} rs The Record(s) to create|update|destroy.
  247. * @param {Object} params The request &lt;code&gt;params&lt;/code&gt; object. Edit &lt;code&gt;params&lt;/code&gt; to add parameters to the request.
  248. */
  249. 'beforewrite',
  250. <span id='Ext-data-DataProxy-event-write'> /**
  251. </span> * @event write
  252. * &lt;p&gt;Fires before the request-callback is called&lt;/p&gt;
  253. * &lt;p&gt;In addition to being fired through the DataProxy instance that raised the event, this event is also fired
  254. * through the Ext.data.DataProxy &lt;i&gt;class&lt;/i&gt; to allow for centralized processing of write events from &lt;b&gt;all&lt;/b&gt;
  255. * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.&lt;/p&gt;
  256. * @param {DataProxy} this The proxy that sent the request
  257. * @param {String} action [Ext.data.Api.actions.create|upate|destroy]
  258. * @param {Object} data The data object extracted from the server-response
  259. * @param {Object} response The decoded response from server
  260. * @param {Record/Record[]} rs The Record(s) from Store
  261. * @param {Object} options The callback's &lt;tt&gt;options&lt;/tt&gt; property as passed to the {@link #request} function
  262. */
  263. 'write'
  264. );
  265. Ext.data.DataProxy.superclass.constructor.call(this);
  266. // Prepare the proxy api. Ensures all API-actions are defined with the Object-form.
  267. try {
  268. Ext.data.Api.prepare(this);
  269. } catch (e) {
  270. if (e instanceof Ext.data.Api.Error) {
  271. e.toConsole();
  272. }
  273. }
  274. // relay each proxy's events onto Ext.data.DataProxy class for centralized Proxy-listening
  275. Ext.data.DataProxy.relayEvents(this, ['beforewrite', 'write', 'exception']);
  276. };
  277. Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {
  278. <span id='Ext-data-DataProxy-cfg-restful'> /**
  279. </span> * @cfg {Boolean} restful
  280. * &lt;p&gt;Defaults to &lt;tt&gt;false&lt;/tt&gt;. Set to &lt;tt&gt;true&lt;/tt&gt; to operate in a RESTful manner.&lt;/p&gt;
  281. * &lt;br&gt;&lt;p&gt; Note: this parameter will automatically be set to &lt;tt&gt;true&lt;/tt&gt; if the
  282. * {@link Ext.data.Store} it is plugged into is set to &lt;code&gt;restful: true&lt;/code&gt;. If the
  283. * Store is RESTful, there is no need to set this option on the proxy.&lt;/p&gt;
  284. * &lt;br&gt;&lt;p&gt;RESTful implementations enable the serverside framework to automatically route
  285. * actions sent to one url based upon the HTTP method, for example:
  286. * &lt;pre&gt;&lt;code&gt;
  287. store: new Ext.data.Store({
  288. restful: true,
  289. proxy: new Ext.data.HttpProxy({url:'/users'}); // all requests sent to /users
  290. ...
  291. )}
  292. * &lt;/code&gt;&lt;/pre&gt;
  293. * If there is no &lt;code&gt;{@link #api}&lt;/code&gt; specified in the configuration of the proxy,
  294. * all requests will be marshalled to a single RESTful url (/users) so the serverside
  295. * framework can inspect the HTTP Method and act accordingly:
  296. * &lt;pre&gt;
  297. &lt;u&gt;Method&lt;/u&gt; &lt;u&gt;url&lt;/u&gt; &lt;u&gt;action&lt;/u&gt;
  298. POST /users create
  299. GET /users read
  300. PUT /users/23 update
  301. DESTROY /users/23 delete
  302. * &lt;/pre&gt;&lt;/p&gt;
  303. * &lt;p&gt;If set to &lt;tt&gt;true&lt;/tt&gt;, a {@link Ext.data.Record#phantom non-phantom} record's
  304. * {@link Ext.data.Record#id id} will be appended to the url. Some MVC (e.g., Ruby on Rails,
  305. * Merb and Django) support segment based urls where the segments in the URL follow the
  306. * Model-View-Controller approach:&lt;pre&gt;&lt;code&gt;
  307. * someSite.com/controller/action/id
  308. * &lt;/code&gt;&lt;/pre&gt;
  309. * Where the segments in the url are typically:&lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  310. * &lt;li&gt;The first segment : represents the controller class that should be invoked.&lt;/li&gt;
  311. * &lt;li&gt;The second segment : represents the class function, or method, that should be called.&lt;/li&gt;
  312. * &lt;li&gt;The third segment : represents the ID (a variable typically passed to the method).&lt;/li&gt;
  313. * &lt;/ul&gt;&lt;/div&gt;&lt;/p&gt;
  314. * &lt;br&gt;&lt;p&gt;Refer to &lt;code&gt;{@link Ext.data.DataProxy#api}&lt;/code&gt; for additional information.&lt;/p&gt;
  315. */
  316. restful: false,
  317. <span id='Ext-data-DataProxy-method-setApi'> /**
  318. </span> * &lt;p&gt;Redefines the Proxy's API or a single action of an API. Can be called with two method signatures.&lt;/p&gt;
  319. * &lt;p&gt;If called with an object as the only parameter, the object should redefine the &lt;b&gt;entire&lt;/b&gt; API, e.g.:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  320. proxy.setApi({
  321. read : '/users/read',
  322. create : '/users/create',
  323. update : '/users/update',
  324. destroy : '/users/destroy'
  325. });
  326. &lt;/code&gt;&lt;/pre&gt;
  327. * &lt;p&gt;If called with two parameters, the first parameter should be a string specifying the API action to
  328. * redefine and the second parameter should be the URL (or function if using DirectProxy) to call for that action, e.g.:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  329. proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
  330. &lt;/code&gt;&lt;/pre&gt;
  331. * @param {String/Object} api An API specification object, or the name of an action.
  332. * @param {String/Function} url The URL (or function if using DirectProxy) to call for the action.
  333. */
  334. setApi : function() {
  335. if (arguments.length == 1) {
  336. var valid = Ext.data.Api.isValid(arguments[0]);
  337. if (valid === true) {
  338. this.api = arguments[0];
  339. }
  340. else {
  341. throw new Ext.data.Api.Error('invalid', valid);
  342. }
  343. }
  344. else if (arguments.length == 2) {
  345. if (!Ext.data.Api.isAction(arguments[0])) {
  346. throw new Ext.data.Api.Error('invalid', arguments[0]);
  347. }
  348. this.api[arguments[0]] = arguments[1];
  349. }
  350. Ext.data.Api.prepare(this);
  351. },
  352. <span id='Ext-data-DataProxy-method-isApiAction'> /**
  353. </span> * Returns true if the specified action is defined as a unique action in the api-config.
  354. * request. If all API-actions are routed to unique urls, the xaction parameter is unecessary. However, if no api is defined
  355. * and all Proxy actions are routed to DataProxy#url, the server-side will require the xaction parameter to perform a switch to
  356. * the corresponding code for CRUD action.
  357. * @param {String} action CREATE READ UPDATE or DESTROY
  358. * @return {Boolean}
  359. */
  360. isApiAction : function(action) {
  361. return (this.api[action]) ? true : false;
  362. },
  363. <span id='Ext-data-DataProxy-method-request'> /**
  364. </span> * All proxy actions are executed through this method. Automatically fires the &quot;before&quot; + action event
  365. * @param {String} action Name of the action
  366. * @param {Ext.data.Record/Ext.data.Record[]/null} rs Will be null when action is 'load'
  367. * @param {Object} params
  368. * @param {Ext.data.DataReader} reader
  369. * @param {Function} callback
  370. * @param {Object} scope The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the callback function is executed. Defaults to the Proxy object.
  371. * @param {Object} options Any options specified for the action (e.g. see {@link Ext.data.Store#load}.
  372. */
  373. request : function(action, rs, params, reader, callback, scope, options) {
  374. if (!this.api[action] &amp;&amp; !this.load) {
  375. throw new Ext.data.DataProxy.Error('action-undefined', action);
  376. }
  377. params = params || {};
  378. if ((action === Ext.data.Api.actions.read) ? this.fireEvent(&quot;beforeload&quot;, this, params) : this.fireEvent(&quot;beforewrite&quot;, this, action, rs, params) !== false) {
  379. this.doRequest.apply(this, arguments);
  380. }
  381. else {
  382. callback.call(scope || this, null, options, false);
  383. }
  384. },
  385. <span id='Ext-data-DataProxy-method-load'> /**
  386. </span> * &lt;b&gt;Deprecated&lt;/b&gt; load method using old method signature. See {@doRequest} for preferred method.
  387. * @deprecated
  388. * @param {Object} params
  389. * @param {Object} reader
  390. * @param {Object} callback
  391. * @param {Object} scope
  392. * @param {Object} arg
  393. */
  394. load : null,
  395. <span id='Ext-data-DataProxy-cfg-doRequest'> /**
  396. </span> * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses. &lt;b&gt;Note:&lt;/b&gt; Should only be used by custom-proxy developers.
  397. * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
  398. * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
  399. */
  400. doRequest : function(action, rs, params, reader, callback, scope, options) {
  401. // default implementation of doRequest for backwards compatibility with 2.0 proxies.
  402. // If we're executing here, the action is probably &quot;load&quot;.
  403. // Call with the pre-3.0 method signature.
  404. this.load(params, reader, callback, scope, options);
  405. },
  406. <span id='Ext-data-DataProxy-cfg-onRead'> /**
  407. </span> * @cfg {Function} onRead Abstract method that should be implemented in all subclasses. &lt;b&gt;Note:&lt;/b&gt; Should only be used by custom-proxy developers. Callback for read {@link Ext.data.Api#actions action}.
  408. * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
  409. * @param {Object} o The request transaction object
  410. * @param {Object} res The server response
  411. * @fires loadexception (deprecated)
  412. * @fires exception
  413. * @fires load
  414. * @protected
  415. */
  416. onRead : Ext.emptyFn,
  417. <span id='Ext-data-DataProxy-cfg-onWrite'> /**
  418. </span> * @cfg {Function} onWrite Abstract method that should be implemented in all subclasses. &lt;b&gt;Note:&lt;/b&gt; Should only be used by custom-proxy developers. Callback for &lt;i&gt;create, update and destroy&lt;/i&gt; {@link Ext.data.Api#actions actions}.
  419. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  420. * @param {Object} trans The request transaction object
  421. * @param {Object} res The server response
  422. * @fires exception
  423. * @fires write
  424. * @protected
  425. */
  426. onWrite : Ext.emptyFn,
  427. <span id='Ext-data-DataProxy-method-buildUrl'> /**
  428. </span> * buildUrl
  429. * Sets the appropriate url based upon the action being executed. If restful is true, and only a single record is being acted upon,
  430. * url will be built Rails-style, as in &quot;/controller/action/32&quot;. restful will aply iff the supplied record is an
  431. * instance of Ext.data.Record rather than an Array of them.
  432. * @param {String} action The api action being executed [read|create|update|destroy]
  433. * @param {Ext.data.Record/Ext.data.Record[]} record The record or Array of Records being acted upon.
  434. * @return {String} url
  435. * @private
  436. */
  437. buildUrl : function(action, record) {
  438. record = record || null;
  439. // conn.url gets nullified after each request. If it's NOT null here, that means the user must have intervened with a call
  440. // to DataProxy#setUrl or DataProxy#setApi and changed it before the request was executed. If that's the case, use conn.url,
  441. // otherwise, build the url from the api or this.url.
  442. var url = (this.conn &amp;&amp; this.conn.url) ? this.conn.url : (this.api[action]) ? this.api[action].url : this.url;
  443. if (!url) {
  444. throw new Ext.data.Api.Error('invalid-url', action);
  445. }
  446. // look for urls having &quot;provides&quot; suffix used in some MVC frameworks like Rails/Merb and others. The provides suffice informs
  447. // the server what data-format the client is dealing with and returns data in the same format (eg: application/json, application/xml, etc)
  448. // e.g.: /users.json, /users.xml, etc.
  449. // with restful routes, we need urls like:
  450. // PUT /users/1.json
  451. // DELETE /users/1.json
  452. var provides = null;
  453. var m = url.match(/(.*)(\.json|\.xml|\.html)$/);
  454. if (m) {
  455. provides = m[2]; // eg &quot;.json&quot;
  456. url = m[1]; // eg: &quot;/users&quot;
  457. }
  458. // prettyUrls is deprectated in favor of restful-config
  459. if ((this.restful === true || this.prettyUrls === true) &amp;&amp; record instanceof Ext.data.Record &amp;&amp; !record.phantom) {
  460. url += '/' + record.id;
  461. }
  462. return (provides === null) ? url : url + provides;
  463. },
  464. <span id='Ext-data-DataProxy-method-destroy'> /**
  465. </span> * Destroys the proxy by purging any event listeners and cancelling any active requests.
  466. */
  467. destroy: function(){
  468. this.purgeListeners();
  469. }
  470. });
  471. // Apply the Observable prototype to the DataProxy class so that proxy instances can relay their
  472. // events to the class. Allows for centralized listening of all proxy instances upon the DataProxy class.
  473. Ext.apply(Ext.data.DataProxy, Ext.util.Observable.prototype);
  474. Ext.util.Observable.call(Ext.data.DataProxy);
  475. <span id='Ext-data-DataProxy-Error'>/**
  476. </span> * @class Ext.data.DataProxy.Error
  477. * @extends Ext.Error
  478. * DataProxy Error extension.
  479. * constructor
  480. * @param {String} message Message describing the error.
  481. * @param {Record/Record[]} arg
  482. */
  483. Ext.data.DataProxy.Error = Ext.extend(Ext.Error, {
  484. <span id='Ext-data-DataProxy-Error-method-constructor'> constructor : function(message, arg) {
  485. </span> this.arg = arg;
  486. Ext.Error.call(this, message);
  487. },
  488. <span id='Ext-data-DataProxy-Error-property-name'> name: 'Ext.data.DataProxy'
  489. </span>});
  490. Ext.apply(Ext.data.DataProxy.Error.prototype, {
  491. lang: {
  492. 'action-undefined': &quot;DataProxy attempted to execute an API-action but found an undefined url / function. Please review your Proxy url/api-configuration.&quot;,
  493. 'api-invalid': 'Recieved an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions from Ext.data.Api.actions.'
  494. }
  495. });
  496. </pre>
  497. </body>
  498. </html>