/ext-4.0.7/src/data/Batch.js

https://bitbucket.org/srogerf/javascript · JavaScript · 193 lines · 68 code · 27 blank · 98 comment · 7 complexity · 6836ad82c57ee827618f5b5848d47a7c MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @author Ed Spencer
  11. * @class Ext.data.Batch
  12. *
  13. * <p>Provides a mechanism to run one or more {@link Ext.data.Operation operations} in a given order. Fires the 'operationcomplete' event
  14. * after the completion of each Operation, and the 'complete' event when all Operations have been successfully executed. Fires an 'exception'
  15. * event if any of the Operations encounter an exception.</p>
  16. *
  17. * <p>Usually these are only used internally by {@link Ext.data.proxy.Proxy} classes</p>
  18. *
  19. */
  20. Ext.define('Ext.data.Batch', {
  21. mixins: {
  22. observable: 'Ext.util.Observable'
  23. },
  24. /**
  25. * @property {Boolean} autoStart
  26. * True to immediately start processing the batch as soon as it is constructed.
  27. */
  28. autoStart: false,
  29. /**
  30. * @property {Number} current
  31. * The index of the current operation being executed
  32. */
  33. current: -1,
  34. /**
  35. * @property {Number} total
  36. * The total number of operations in this batch. Read only
  37. */
  38. total: 0,
  39. /**
  40. * @property {Boolean} isRunning
  41. * True if the batch is currently running
  42. */
  43. isRunning: false,
  44. /**
  45. * @property {Boolean} isComplete
  46. * True if this batch has been executed completely
  47. */
  48. isComplete: false,
  49. /**
  50. * @property {Boolean} hasException
  51. * True if this batch has encountered an exception. This is cleared at the start of each operation
  52. */
  53. hasException: false,
  54. /**
  55. * @property {Boolean} pauseOnException
  56. * True to automatically pause the execution of the batch if any operation encounters an exception
  57. */
  58. pauseOnException: true,
  59. /**
  60. * Creates new Batch object.
  61. * @param {Object} [config] Config object
  62. */
  63. constructor: function(config) {
  64. var me = this;
  65. me.addEvents(
  66. /**
  67. * @event complete
  68. * Fired when all operations of this batch have been completed
  69. * @param {Ext.data.Batch} batch The batch object
  70. * @param {Object} operation The last operation that was executed
  71. */
  72. 'complete',
  73. /**
  74. * @event exception
  75. * Fired when a operation encountered an exception
  76. * @param {Ext.data.Batch} batch The batch object
  77. * @param {Object} operation The operation that encountered the exception
  78. */
  79. 'exception',
  80. /**
  81. * @event operationcomplete
  82. * Fired when each operation of the batch completes
  83. * @param {Ext.data.Batch} batch The batch object
  84. * @param {Object} operation The operation that just completed
  85. */
  86. 'operationcomplete'
  87. );
  88. me.mixins.observable.constructor.call(me, config);
  89. /**
  90. * Ordered array of operations that will be executed by this batch
  91. * @property {Ext.data.Operation[]} operations
  92. */
  93. me.operations = [];
  94. },
  95. /**
  96. * Adds a new operation to this batch
  97. * @param {Object} operation The {@link Ext.data.Operation Operation} object
  98. */
  99. add: function(operation) {
  100. this.total++;
  101. operation.setBatch(this);
  102. this.operations.push(operation);
  103. },
  104. /**
  105. * Kicks off the execution of the batch, continuing from the next operation if the previous
  106. * operation encountered an exception, or if execution was paused
  107. */
  108. start: function() {
  109. this.hasException = false;
  110. this.isRunning = true;
  111. this.runNextOperation();
  112. },
  113. /**
  114. * @private
  115. * Runs the next operation, relative to this.current.
  116. */
  117. runNextOperation: function() {
  118. this.runOperation(this.current + 1);
  119. },
  120. /**
  121. * Pauses execution of the batch, but does not cancel the current operation
  122. */
  123. pause: function() {
  124. this.isRunning = false;
  125. },
  126. /**
  127. * Executes a operation by its numeric index
  128. * @param {Number} index The operation index to run
  129. */
  130. runOperation: function(index) {
  131. var me = this,
  132. operations = me.operations,
  133. operation = operations[index],
  134. onProxyReturn;
  135. if (operation === undefined) {
  136. me.isRunning = false;
  137. me.isComplete = true;
  138. me.fireEvent('complete', me, operations[operations.length - 1]);
  139. } else {
  140. me.current = index;
  141. onProxyReturn = function(operation) {
  142. var hasException = operation.hasException();
  143. if (hasException) {
  144. me.hasException = true;
  145. me.fireEvent('exception', me, operation);
  146. } else {
  147. me.fireEvent('operationcomplete', me, operation);
  148. }
  149. if (hasException && me.pauseOnException) {
  150. me.pause();
  151. } else {
  152. operation.setCompleted();
  153. me.runNextOperation();
  154. }
  155. };
  156. operation.setStarted();
  157. me.proxy[operation.action](operation, onProxyReturn, me);
  158. }
  159. }
  160. });