Batch.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-Batch'>/**
  19. </span> * @author Ed Spencer
  20. * @class Ext.data.Batch
  21. *
  22. * &lt;p&gt;Provides a mechanism to run one or more {@link Ext.data.Operation operations} in a given order. Fires the 'operationcomplete' event
  23. * after the completion of each Operation, and the 'complete' event when all Operations have been successfully executed. Fires an 'exception'
  24. * event if any of the Operations encounter an exception.&lt;/p&gt;
  25. *
  26. * &lt;p&gt;Usually these are only used internally by {@link Ext.data.proxy.Proxy} classes&lt;/p&gt;
  27. *
  28. */
  29. Ext.define('Ext.data.Batch', {
  30. mixins: {
  31. observable: 'Ext.util.Observable'
  32. },
  33. <span id='Ext-data-Batch-cfg-autoStart'> /**
  34. </span> * @cfg {Boolean} autoStart
  35. * True to immediately start processing the batch as soon as it is constructed (defaults to false)
  36. */
  37. autoStart: false,
  38. <span id='Ext-data-Batch-cfg-pauseOnException'> /**
  39. </span> * @cfg {Boolean} pauseOnException
  40. * True to pause the execution of the batch if any operation encounters an exception
  41. * (defaults to false). If you set this to true you are responsible for implementing the appropriate
  42. * handling logic and restarting or discarding the batch as needed. There are different ways you could
  43. * do this, e.g. by handling the batch's {@link #exception} event directly, or perhaps by overriding
  44. * {@link Ext.data.AbstractStore#onBatchException onBatchException} at the store level. If you do pause
  45. * and attempt to handle the exception you can call {@link #retry} to process the same operation again.
  46. *
  47. * Note that {@link Ext.data.Operation operations} are atomic, so any operations that may have succeeded
  48. * prior to an exception (and up until pausing the batch) will be finalized at the server level and will
  49. * not be automatically reversible. Any transactional / rollback behavior that might be desired would have
  50. * to be implemented at the application level. Pausing on exception will likely be most beneficial when
  51. * used in coordination with such a scheme, where an exception might actually affect subsequent operations
  52. * in the same batch and so should be handled before continuing with the next operation.
  53. *
  54. * If you have not implemented transactional operation handling then this option should typically be left
  55. * to the default of false (e.g. process as many operations as possible, and handle any exceptions
  56. * asynchronously without holding up the rest of the batch).
  57. */
  58. pauseOnException: false,
  59. <span id='Ext-data-Batch-property-current'> /**
  60. </span> * @property {Number} current
  61. * The index of the current operation being executed. Read only
  62. */
  63. current: -1,
  64. <span id='Ext-data-Batch-property-total'> /**
  65. </span> * @property {Number} total
  66. * The total number of operations in this batch. Read only
  67. */
  68. total: 0,
  69. <span id='Ext-data-Batch-property-isRunning'> /**
  70. </span> * @property {Boolean} isRunning
  71. * True if the batch is currently running. Read only
  72. */
  73. isRunning: false,
  74. <span id='Ext-data-Batch-property-isComplete'> /**
  75. </span> * @property {Boolean} isComplete
  76. * True if this batch has been executed completely. Read only
  77. */
  78. isComplete: false,
  79. <span id='Ext-data-Batch-property-hasException'> /**
  80. </span> * @property {Boolean} hasException
  81. * True if this batch has encountered an exception. This is cleared at the start of each operation. Read only
  82. */
  83. hasException: false,
  84. <span id='Ext-data-Batch-method-constructor'> /**
  85. </span> * Creates new Batch object.
  86. * @param {Object} [config] Config object
  87. */
  88. constructor: function(config) {
  89. var me = this;
  90. <span id='Ext-data-Batch-event-complete'> /**
  91. </span> * @event complete
  92. * Fired when all operations of this batch have been completed
  93. * @param {Ext.data.Batch} batch The batch object
  94. * @param {Object} operation The last operation that was executed
  95. */
  96. <span id='Ext-data-Batch-event-exception'> /**
  97. </span> * @event exception
  98. * Fired when a operation encountered an exception
  99. * @param {Ext.data.Batch} batch The batch object
  100. * @param {Object} operation The operation that encountered the exception
  101. */
  102. <span id='Ext-data-Batch-event-operationcomplete'> /**
  103. </span> * @event operationcomplete
  104. * Fired when each operation of the batch completes
  105. * @param {Ext.data.Batch} batch The batch object
  106. * @param {Object} operation The operation that just completed
  107. */
  108. me.mixins.observable.constructor.call(me, config);
  109. <span id='Ext-data-Batch-property-operations'> /**
  110. </span> * Ordered array of operations that will be executed by this batch
  111. * @property {Ext.data.Operation[]} operations
  112. */
  113. me.operations = [];
  114. <span id='Ext-data-Batch-property-exceptions'> /**
  115. </span> * Ordered array of operations that raised an exception during the most recent
  116. * batch execution and did not successfully complete
  117. * @property {Ext.data.Operation[]} exceptions
  118. */
  119. me.exceptions = [];
  120. },
  121. <span id='Ext-data-Batch-method-add'> /**
  122. </span> * Adds a new operation to this batch at the end of the {@link #operations} array
  123. * @param {Object} operation The {@link Ext.data.Operation Operation} object
  124. * @return {Ext.data.Batch} this
  125. */
  126. add: function(operation) {
  127. this.total++;
  128. operation.setBatch(this);
  129. this.operations.push(operation);
  130. return this;
  131. },
  132. <span id='Ext-data-Batch-method-start'> /**
  133. </span> * Kicks off execution of the batch, continuing from the next operation if the previous
  134. * operation encountered an exception, or if execution was paused. Use this method to start
  135. * the batch for the first time or to restart a paused batch by skipping the current
  136. * unsuccessful operation.
  137. *
  138. * To retry processing the current operation before continuing to the rest of the batch (e.g.
  139. * because you explicitly handled the operation's exception), call {@link #retry} instead.
  140. *
  141. * Note that if the batch is already running any call to start will be ignored.
  142. *
  143. * @return {Ext.data.Batch} this
  144. */
  145. start: function(/* private */ index) {
  146. var me = this;
  147. if (me.isRunning) {
  148. return me;
  149. }
  150. me.exceptions.length = 0;
  151. me.hasException = false;
  152. me.isRunning = true;
  153. return me.runOperation(Ext.isDefined(index) ? index : me.current + 1);
  154. },
  155. <span id='Ext-data-Batch-method-retry'> /**
  156. </span> * Kicks off execution of the batch, continuing from the current operation. This is intended
  157. * for restarting a {@link #pause paused} batch after an exception, and the operation that raised
  158. * the exception will now be retried. The batch will then continue with its normal processing until
  159. * all operations are complete or another exception is encountered.
  160. *
  161. * Note that if the batch is already running any call to retry will be ignored.
  162. *
  163. * @return {Ext.data.Batch} this
  164. */
  165. retry: function() {
  166. return this.start(this.current);
  167. },
  168. <span id='Ext-data-Batch-method-runNextOperation'> /**
  169. </span> * @private
  170. * Runs the next operation, relative to this.current.
  171. * @return {Ext.data.Batch} this
  172. */
  173. runNextOperation: function() {
  174. return this.runOperation(this.current + 1);
  175. },
  176. <span id='Ext-data-Batch-method-pause'> /**
  177. </span> * Pauses execution of the batch, but does not cancel the current operation
  178. * @return {Ext.data.Batch} this
  179. */
  180. pause: function() {
  181. this.isRunning = false;
  182. return this;
  183. },
  184. <span id='Ext-data-Batch-method-runOperation'> /**
  185. </span> * Executes an operation by its numeric index in the {@link #operations} array
  186. * @param {Number} index The operation index to run
  187. * @return {Ext.data.Batch} this
  188. */
  189. runOperation: function(index) {
  190. var me = this,
  191. operations = me.operations,
  192. operation = operations[index],
  193. onProxyReturn;
  194. if (operation === undefined) {
  195. me.isRunning = false;
  196. me.isComplete = true;
  197. me.fireEvent('complete', me, operations[operations.length - 1]);
  198. } else {
  199. me.current = index;
  200. onProxyReturn = function(operation) {
  201. var hasException = operation.hasException();
  202. if (hasException) {
  203. me.hasException = true;
  204. me.exceptions.push(operation);
  205. me.fireEvent('exception', me, operation);
  206. }
  207. if (hasException &amp;&amp; me.pauseOnException) {
  208. me.pause();
  209. } else {
  210. operation.setCompleted();
  211. me.fireEvent('operationcomplete', me, operation);
  212. me.runNextOperation();
  213. }
  214. };
  215. operation.setStarted();
  216. me.proxy[operation.action](operation, onProxyReturn, me);
  217. }
  218. return me;
  219. }
  220. });
  221. </pre>
  222. </body>
  223. </html>