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.

299 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">
  19. <span id='Ext-data-Api'>/**
  20. </span> * @class Ext.data.Api
  21. * @extends Object
  22. * Ext.data.Api is a singleton designed to manage the data API including methods
  23. * for validating a developer's DataProxy API. Defines variables for CRUD actions
  24. * create, read, update and destroy in addition to a mapping of RESTful HTTP methods
  25. * GET, POST, PUT and DELETE to CRUD actions.
  26. * @singleton
  27. */
  28. Ext.data.Api = (function() {
  29. // private validActions. validActions is essentially an inverted hash of Ext.data.Api.actions, where value becomes the key.
  30. // Some methods in this singleton (e.g.: getActions, getVerb) will loop through actions with the code &lt;code&gt;for (var verb in this.actions)&lt;/code&gt;
  31. // For efficiency, some methods will first check this hash for a match. Those methods which do acces validActions will cache their result here.
  32. // We cannot pre-define this hash since the developer may over-ride the actions at runtime.
  33. var validActions = {};
  34. return {
  35. <span id='Ext-data-Api-property-actions'> /**
  36. </span> * Defined actions corresponding to remote actions:
  37. * &lt;pre&gt;&lt;code&gt;
  38. actions: {
  39. create : 'create', // Text representing the remote-action to create records on server.
  40. read : 'read', // Text representing the remote-action to read/load data from server.
  41. update : 'update', // Text representing the remote-action to update records on server.
  42. destroy : 'destroy' // Text representing the remote-action to destroy records on server.
  43. }
  44. * &lt;/code&gt;&lt;/pre&gt;
  45. * @property actions
  46. * @type Object
  47. */
  48. actions : {
  49. create : 'create',
  50. read : 'read',
  51. update : 'update',
  52. destroy : 'destroy'
  53. },
  54. <span id='Ext-data-Api-property-restActions'> /**
  55. </span> * Defined {CRUD action}:{HTTP method} pairs to associate HTTP methods with the
  56. * corresponding actions for {@link Ext.data.DataProxy#restful RESTful proxies}.
  57. * Defaults to:
  58. * &lt;pre&gt;&lt;code&gt;
  59. restActions : {
  60. create : 'POST',
  61. read : 'GET',
  62. update : 'PUT',
  63. destroy : 'DELETE'
  64. },
  65. * &lt;/code&gt;&lt;/pre&gt;
  66. */
  67. restActions : {
  68. create : 'POST',
  69. read : 'GET',
  70. update : 'PUT',
  71. destroy : 'DELETE'
  72. },
  73. <span id='Ext-data-Api-method-isAction'> /**
  74. </span> * Returns true if supplied action-name is a valid API action defined in &lt;code&gt;{@link #actions}&lt;/code&gt; constants
  75. * @param {String} action Action to test for availability.
  76. * @return {Boolean}
  77. */
  78. isAction : function(action) {
  79. return (Ext.data.Api.actions[action]) ? true : false;
  80. },
  81. <span id='Ext-data-Api-method-getVerb'> /**
  82. </span> * Returns the actual CRUD action KEY &quot;create&quot;, &quot;read&quot;, &quot;update&quot; or &quot;destroy&quot; from the supplied action-name. This method is used internally and shouldn't generally
  83. * need to be used directly. The key/value pair of Ext.data.Api.actions will often be identical but this is not necessarily true. A developer can override this naming
  84. * convention if desired. However, the framework internally calls methods based upon the KEY so a way of retreiving the the words &quot;create&quot;, &quot;read&quot;, &quot;update&quot; and &quot;destroy&quot; is
  85. * required. This method will cache discovered KEYS into the private validActions hash.
  86. * @param {String} name The runtime name of the action.
  87. * @return {String/null} returns the action-key, or verb of the user-action or null if invalid.
  88. * @nodoc
  89. */
  90. getVerb : function(name) {
  91. if (validActions[name]) {
  92. return validActions[name]; // &lt;-- found in cache. return immediately.
  93. }
  94. for (var verb in this.actions) {
  95. if (this.actions[verb] === name) {
  96. validActions[name] = verb;
  97. break;
  98. }
  99. }
  100. return (validActions[name] !== undefined) ? validActions[name] : null;
  101. },
  102. <span id='Ext-data-Api-method-isValid'> /**
  103. </span> * Returns true if the supplied API is valid; that is, check that all keys match defined actions
  104. * otherwise returns an array of mistakes.
  105. * @return {String[]|true}
  106. */
  107. isValid : function(api){
  108. var invalid = [];
  109. var crud = this.actions; // &lt;-- cache a copy of the actions.
  110. for (var action in api) {
  111. if (!(action in crud)) {
  112. invalid.push(action);
  113. }
  114. }
  115. return (!invalid.length) ? true : invalid;
  116. },
  117. <span id='Ext-data-Api-method-hasUniqueUrl'> /**
  118. </span> * Returns true if the supplied verb upon the supplied proxy points to a unique url in that none of the other api-actions
  119. * point to the same url. The question is important for deciding whether to insert the &quot;xaction&quot; HTTP parameter within an
  120. * Ajax request. This method is used internally and shouldn't generally need to be called directly.
  121. * @param {Ext.data.DataProxy} proxy
  122. * @param {String} verb
  123. * @return {Boolean}
  124. */
  125. hasUniqueUrl : function(proxy, verb) {
  126. var url = (proxy.api[verb]) ? proxy.api[verb].url : null;
  127. var unique = true;
  128. for (var action in proxy.api) {
  129. if ((unique = (action === verb) ? true : (proxy.api[action].url != url) ? true : false) === false) {
  130. break;
  131. }
  132. }
  133. return unique;
  134. },
  135. <span id='Ext-data-Api-method-prepare'> /**
  136. </span> * This method is used internally by &lt;tt&gt;{@link Ext.data.DataProxy DataProxy}&lt;/tt&gt; and should not generally need to be used directly.
  137. * Each action of a DataProxy api can be initially defined as either a String or an Object. When specified as an object,
  138. * one can explicitly define the HTTP method (GET|POST) to use for each CRUD action. This method will prepare the supplied API, setting
  139. * each action to the Object form. If your API-actions do not explicitly define the HTTP method, the &quot;method&quot; configuration-parameter will
  140. * be used. If the method configuration parameter is not specified, POST will be used.
  141. &lt;pre&gt;&lt;code&gt;
  142. new Ext.data.HttpProxy({
  143. method: &quot;POST&quot;, // &lt;-- default HTTP method when not specified.
  144. api: {
  145. create: 'create.php',
  146. load: 'read.php',
  147. save: 'save.php',
  148. destroy: 'destroy.php'
  149. }
  150. });
  151. // Alternatively, one can use the object-form to specify the API
  152. new Ext.data.HttpProxy({
  153. api: {
  154. load: {url: 'read.php', method: 'GET'},
  155. create: 'create.php',
  156. destroy: 'destroy.php',
  157. save: 'update.php'
  158. }
  159. });
  160. &lt;/code&gt;&lt;/pre&gt;
  161. *
  162. * @param {Ext.data.DataProxy} proxy
  163. */
  164. prepare : function(proxy) {
  165. if (!proxy.api) {
  166. proxy.api = {}; // &lt;-- No api? create a blank one.
  167. }
  168. for (var verb in this.actions) {
  169. var action = this.actions[verb];
  170. proxy.api[action] = proxy.api[action] || proxy.url || proxy.directFn;
  171. if (typeof(proxy.api[action]) == 'string') {
  172. proxy.api[action] = {
  173. url: proxy.api[action],
  174. method: (proxy.restful === true) ? Ext.data.Api.restActions[action] : undefined
  175. };
  176. }
  177. }
  178. },
  179. <span id='Ext-data-Api-method-restify'> /**
  180. </span> * Prepares a supplied Proxy to be RESTful. Sets the HTTP method for each api-action to be one of
  181. * GET, POST, PUT, DELETE according to the defined {@link #restActions}.
  182. * @param {Ext.data.DataProxy} proxy
  183. */
  184. restify : function(proxy) {
  185. proxy.restful = true;
  186. for (var verb in this.restActions) {
  187. proxy.api[this.actions[verb]].method ||
  188. (proxy.api[this.actions[verb]].method = this.restActions[verb]);
  189. }
  190. // TODO: perhaps move this interceptor elsewhere? like into DataProxy, perhaps? Placed here
  191. // to satisfy initial 3.0 final release of REST features.
  192. proxy.onWrite = proxy.onWrite.createInterceptor(function(action, o, response, rs) {
  193. var reader = o.reader;
  194. var res = new Ext.data.Response({
  195. action: action,
  196. raw: response
  197. });
  198. switch (response.status) {
  199. case 200: // standard 200 response, send control back to HttpProxy#onWrite by returning true from this intercepted #onWrite
  200. return true;
  201. break;
  202. case 201: // entity created but no response returned
  203. if (Ext.isEmpty(res.raw.responseText)) {
  204. res.success = true;
  205. } else {
  206. //if the response contains data, treat it like a 200
  207. return true;
  208. }
  209. break;
  210. case 204: // no-content. Create a fake response.
  211. res.success = true;
  212. res.data = null;
  213. break;
  214. default:
  215. return true;
  216. break;
  217. }
  218. if (res.success === true) {
  219. this.fireEvent(&quot;write&quot;, this, action, res.data, res, rs, o.request.arg);
  220. } else {
  221. this.fireEvent('exception', this, 'remote', action, o, res, rs);
  222. }
  223. o.request.callback.call(o.request.scope, res.data, res, res.success);
  224. return false; // &lt;-- false to prevent intercepted function from running.
  225. }, proxy);
  226. }
  227. };
  228. })();
  229. <span id='Ext-data-Response'>/**
  230. </span> * Ext.data.Response
  231. * Experimental. Do not use directly.
  232. */
  233. Ext.data.Response = function(params, response) {
  234. Ext.apply(this, params, {
  235. raw: response
  236. });
  237. };
  238. Ext.data.Response.prototype = {
  239. message : null,
  240. success : false,
  241. status : null,
  242. root : null,
  243. raw : null,
  244. getMessage : function() {
  245. return this.message;
  246. },
  247. getSuccess : function() {
  248. return this.success;
  249. },
  250. getStatus : function() {
  251. return this.status;
  252. },
  253. getRoot : function() {
  254. return this.root;
  255. },
  256. getRawResponse : function() {
  257. return this.raw;
  258. }
  259. };
  260. <span id='Ext-data-Api-Error'>/**
  261. </span> * @class Ext.data.Api.Error
  262. * @extends Ext.Error
  263. * Error class for Ext.data.Api errors
  264. */
  265. Ext.data.Api.Error = Ext.extend(Ext.Error, {
  266. <span id='Ext-data-Api-Error-method-constructor'> constructor : function(message, arg) {
  267. </span> this.arg = arg;
  268. Ext.Error.call(this, message);
  269. },
  270. <span id='Ext-data-Api-Error-property-name'> name: 'Ext.data.Api'
  271. </span>});
  272. Ext.apply(Ext.data.Api.Error.prototype, {
  273. lang: {
  274. 'action-url-undefined': 'No fallback url defined for this action. When defining a DataProxy api, please be sure to define an url for each CRUD action in Ext.data.Api.actions or define a default url in addition to your api-configuration.',
  275. 'invalid': 'received an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions defined in Ext.data.Api.actions',
  276. 'invalid-url': 'Invalid url. Please review your proxy configuration.',
  277. 'execute': 'Attempted to execute an unknown action. Valid API actions are defined in Ext.data.Api.actions&quot;'
  278. }
  279. });
  280. </pre>
  281. </body>
  282. </html>