Operation.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-Operation'>/**
  19. </span> * @author Ed Spencer
  20. *
  21. * Represents a single read or write operation performed by a {@link Ext.data.proxy.Proxy Proxy}. Operation objects are
  22. * used to enable communication between Stores and Proxies. Application developers should rarely need to interact with
  23. * Operation objects directly.
  24. *
  25. * Several Operations can be batched together in a {@link Ext.data.Batch batch}.
  26. */
  27. Ext.define('Ext.data.Operation', {
  28. <span id='Ext-data-Operation-cfg-synchronous'> /**
  29. </span> * @cfg {Boolean} synchronous
  30. * True if this Operation is to be executed synchronously. This property is inspected by a
  31. * {@link Ext.data.Batch Batch} to see if a series of Operations can be executed in parallel or not.
  32. */
  33. synchronous: true,
  34. <span id='Ext-data-Operation-cfg-action'> /**
  35. </span> * @cfg {String} action
  36. * The action being performed by this Operation. Should be one of 'create', 'read', 'update' or 'destroy'.
  37. */
  38. action: undefined,
  39. <span id='Ext-data-Operation-cfg-filters'> /**
  40. </span> * @cfg {Ext.util.Filter[]} filters
  41. * Optional array of filter objects. Only applies to 'read' actions.
  42. */
  43. filters: undefined,
  44. <span id='Ext-data-Operation-cfg-sorters'> /**
  45. </span> * @cfg {Ext.util.Sorter[]} sorters
  46. * Optional array of sorter objects. Only applies to 'read' actions.
  47. */
  48. sorters: undefined,
  49. <span id='Ext-data-Operation-cfg-groupers'> /**
  50. </span> * @cfg {Ext.util.Grouper[]} groupers
  51. * Optional grouping configuration. Only applies to 'read' actions where grouping is desired.
  52. */
  53. groupers: undefined,
  54. <span id='Ext-data-Operation-cfg-start'> /**
  55. </span> * @cfg {Number} start
  56. * The start index (offset), used in paging when running a 'read' action.
  57. */
  58. start: undefined,
  59. <span id='Ext-data-Operation-cfg-limit'> /**
  60. </span> * @cfg {Number} limit
  61. * The number of records to load. Used on 'read' actions when paging is being used.
  62. */
  63. limit: undefined,
  64. <span id='Ext-data-Operation-cfg-batch'> /**
  65. </span> * @cfg {Ext.data.Batch} batch
  66. * The batch that this Operation is a part of.
  67. */
  68. batch: undefined,
  69. <span id='Ext-data-Operation-cfg-params'> /**
  70. </span> * @cfg {Object} params
  71. * Parameters to pass along with the request when performing the operation.
  72. */
  73. <span id='Ext-data-Operation-cfg-callback'> /**
  74. </span> * @cfg {Function} callback
  75. * Function to execute when operation completed.
  76. * @cfg {Ext.data.Model[]} callback.records Array of records.
  77. * @cfg {Ext.data.Operation} callback.operation The Operation itself.
  78. * @cfg {Boolean} callback.success True when operation completed successfully.
  79. */
  80. callback: undefined,
  81. <span id='Ext-data-Operation-cfg-scope'> /**
  82. </span> * @cfg {Object} scope
  83. * Scope for the {@link #callback} function.
  84. */
  85. scope: undefined,
  86. <span id='Ext-data-Operation-property-started'> /**
  87. </span> * @property {Boolean} started
  88. * The start status of this Operation. Use {@link #isStarted}.
  89. * @readonly
  90. * @private
  91. */
  92. started: false,
  93. <span id='Ext-data-Operation-property-running'> /**
  94. </span> * @property {Boolean} running
  95. * The run status of this Operation. Use {@link #isRunning}.
  96. * @readonly
  97. * @private
  98. */
  99. running: false,
  100. <span id='Ext-data-Operation-property-complete'> /**
  101. </span> * @property {Boolean} complete
  102. * The completion status of this Operation. Use {@link #isComplete}.
  103. * @readonly
  104. * @private
  105. */
  106. complete: false,
  107. <span id='Ext-data-Operation-property-success'> /**
  108. </span> * @property {Boolean} success
  109. * Whether the Operation was successful or not. This starts as undefined and is set to true
  110. * or false by the Proxy that is executing the Operation. It is also set to false by {@link #setException}. Use
  111. * {@link #wasSuccessful} to query success status.
  112. * @readonly
  113. * @private
  114. */
  115. success: undefined,
  116. <span id='Ext-data-Operation-property-exception'> /**
  117. </span> * @property {Boolean} exception
  118. * The exception status of this Operation. Use {@link #hasException} and see {@link #getError}.
  119. * @readonly
  120. * @private
  121. */
  122. exception: false,
  123. <span id='Ext-data-Operation-property-error'> /**
  124. </span> * @property {String/Object} error
  125. * The error object passed when {@link #setException} was called. This could be any object or primitive.
  126. * @private
  127. */
  128. error: undefined,
  129. <span id='Ext-data-Operation-property-actionCommitRecordsRe'> /**
  130. </span> * @property {RegExp} actionCommitRecordsRe
  131. * The RegExp used to categorize actions that require record commits.
  132. */
  133. actionCommitRecordsRe: /^(?:create|update)$/i,
  134. <span id='Ext-data-Operation-property-actionSkipSyncRe'> /**
  135. </span> * @property {RegExp} actionSkipSyncRe
  136. * The RegExp used to categorize actions that skip local record synchronization. This defaults
  137. * to match 'destroy'.
  138. */
  139. actionSkipSyncRe: /^destroy$/i,
  140. <span id='Ext-data-Operation-method-constructor'> /**
  141. </span> * Creates new Operation object.
  142. * @param {Object} config (optional) Config object.
  143. */
  144. constructor: function(config) {
  145. Ext.apply(this, config || {});
  146. },
  147. <span id='Ext-data-Operation-method-commitRecords'> /**
  148. </span> * This method is called to commit data to this instance's records given the records in
  149. * the server response. This is followed by calling {@link Ext.data.Model#commit} on all
  150. * those records (for 'create' and 'update' actions).
  151. *
  152. * If this {@link #action} is 'destroy', any server records are ignored and the
  153. * {@link Ext.data.Model#commit} method is not called.
  154. *
  155. * @param {Ext.data.Model[]} serverRecords An array of {@link Ext.data.Model} objects returned by
  156. * the server.
  157. * @markdown
  158. */
  159. commitRecords: function (serverRecords) {
  160. var me = this,
  161. mc, index, clientRecords, serverRec, clientRec, i, len;
  162. if (!me.actionSkipSyncRe.test(me.action)) {
  163. clientRecords = me.records;
  164. if (clientRecords &amp;&amp; clientRecords.length) {
  165. if (clientRecords.length &gt; 1) {
  166. // If this operation has multiple records, client records need to be matched up with server records
  167. // so that any data returned from the server can be updated in the client records. If we don't have
  168. // a clientIdProperty specified on the model and we've done a create, just assume the data is returned in order.
  169. // If it's an update, the records should already have an id which should match what the server returns.
  170. if (me.action == 'update' || clientRecords[0].clientIdProperty) {
  171. mc = new Ext.util.MixedCollection();
  172. mc.addAll(serverRecords);
  173. for (index = clientRecords.length; index--; ) {
  174. clientRec = clientRecords[index];
  175. serverRec = mc.findBy(me.matchClientRec, clientRec);
  176. // Replace client record data with server record data
  177. clientRec.copyFrom(serverRec);
  178. }
  179. } else {
  180. for (i = 0, len = clientRecords.length; i &lt; len; ++i) {
  181. clientRec = clientRecords[i];
  182. serverRec = serverRecords[i];
  183. if (clientRec &amp;&amp; serverRec) {
  184. me.updateRecord(clientRec, serverRec);
  185. }
  186. }
  187. }
  188. } else {
  189. // operation only has one record, so just match the first client record up with the first server record
  190. this.updateRecord(clientRecords[0], serverRecords[0]);
  191. }
  192. if (me.actionCommitRecordsRe.test(me.action)) {
  193. for (index = clientRecords.length; index--; ) {
  194. clientRecords[index].commit();
  195. }
  196. }
  197. }
  198. }
  199. },
  200. updateRecord: function(clientRec, serverRec) {
  201. // if the client record is not a phantom, make sure the ids match before replacing the client data with server data.
  202. if(serverRec &amp;&amp; (clientRec.phantom || clientRec.getId() === serverRec.getId())) {
  203. clientRec.copyFrom(serverRec);
  204. }
  205. },
  206. // Private.
  207. // Record matching function used by commitRecords
  208. // IMPORTANT: This is called in the scope of the clientRec being matched
  209. matchClientRec: function(record) {
  210. var clientRec = this,
  211. clientRecordId = clientRec.getId();
  212. if(clientRecordId &amp;&amp; record.getId() === clientRecordId) {
  213. return true;
  214. }
  215. // if the server record cannot be found by id, find by internalId.
  216. // this allows client records that did not previously exist on the server
  217. // to be updated with the correct server id and data.
  218. return record.internalId === clientRec.internalId;
  219. },
  220. <span id='Ext-data-Operation-method-setStarted'> /**
  221. </span> * Marks the Operation as started.
  222. */
  223. setStarted: function() {
  224. this.started = true;
  225. this.running = true;
  226. },
  227. <span id='Ext-data-Operation-method-setCompleted'> /**
  228. </span> * Marks the Operation as completed.
  229. */
  230. setCompleted: function() {
  231. this.complete = true;
  232. this.running = false;
  233. },
  234. <span id='Ext-data-Operation-method-setSuccessful'> /**
  235. </span> * Marks the Operation as successful.
  236. */
  237. setSuccessful: function() {
  238. this.success = true;
  239. },
  240. <span id='Ext-data-Operation-method-setException'> /**
  241. </span> * Marks the Operation as having experienced an exception. Can be supplied with an option error message/object.
  242. * @param {String/Object} error (optional) error string/object
  243. */
  244. setException: function(error) {
  245. this.exception = true;
  246. this.success = false;
  247. this.running = false;
  248. this.error = error;
  249. },
  250. <span id='Ext-data-Operation-method-hasException'> /**
  251. </span> * Returns true if this Operation encountered an exception (see also {@link #getError})
  252. * @return {Boolean} True if there was an exception
  253. */
  254. hasException: function() {
  255. return this.exception === true;
  256. },
  257. <span id='Ext-data-Operation-method-getError'> /**
  258. </span> * Returns the error string or object that was set using {@link #setException}
  259. * @return {String/Object} The error object
  260. */
  261. getError: function() {
  262. return this.error;
  263. },
  264. <span id='Ext-data-Operation-method-getRecords'> /**
  265. </span> * Returns the {@link Ext.data.Model record}s associated with this operation. For read operations the records as set by the {@link Ext.data.proxy.Proxy Proxy} will be returned (returns `null` if the proxy has not yet set the records).
  266. * For create, update, and destroy operations the operation's initially configured records will be returned, although the proxy may modify these records' data at some point after the operation is initialized.
  267. * @return {Ext.data.Model[]}
  268. */
  269. getRecords: function() {
  270. var resultSet = this.getResultSet();
  271. return this.records || (resultSet ? resultSet.records : null);
  272. },
  273. <span id='Ext-data-Operation-method-getResultSet'> /**
  274. </span> * Returns the ResultSet object (if set by the Proxy). This object will contain the {@link Ext.data.Model model}
  275. * instances as well as meta data such as number of instances fetched, number available etc
  276. * @return {Ext.data.ResultSet} The ResultSet object
  277. */
  278. getResultSet: function() {
  279. return this.resultSet;
  280. },
  281. <span id='Ext-data-Operation-method-isStarted'> /**
  282. </span> * Returns true if the Operation has been started. Note that the Operation may have started AND completed, see
  283. * {@link #isRunning} to test if the Operation is currently running.
  284. * @return {Boolean} True if the Operation has started
  285. */
  286. isStarted: function() {
  287. return this.started === true;
  288. },
  289. <span id='Ext-data-Operation-method-isRunning'> /**
  290. </span> * Returns true if the Operation has been started but has not yet completed.
  291. * @return {Boolean} True if the Operation is currently running
  292. */
  293. isRunning: function() {
  294. return this.running === true;
  295. },
  296. <span id='Ext-data-Operation-method-isComplete'> /**
  297. </span> * Returns true if the Operation has been completed
  298. * @return {Boolean} True if the Operation is complete
  299. */
  300. isComplete: function() {
  301. return this.complete === true;
  302. },
  303. <span id='Ext-data-Operation-method-wasSuccessful'> /**
  304. </span> * Returns true if the Operation has completed and was successful
  305. * @return {Boolean} True if successful
  306. */
  307. wasSuccessful: function() {
  308. return this.isComplete() &amp;&amp; this.success === true;
  309. },
  310. <span id='Ext-data-Operation-method-setBatch'> /**
  311. </span> * @private
  312. * Associates this Operation with a Batch
  313. * @param {Ext.data.Batch} batch The batch
  314. */
  315. setBatch: function(batch) {
  316. this.batch = batch;
  317. },
  318. <span id='Ext-data-Operation-method-allowWrite'> /**
  319. </span> * Checks whether this operation should cause writing to occur.
  320. * @return {Boolean} Whether the operation should cause a write to occur.
  321. */
  322. allowWrite: function() {
  323. return this.action != 'read';
  324. }
  325. });
  326. </pre>
  327. </body>
  328. </html>