3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
17 * @class Ext.data.Batch
19 * <p>Provides a mechanism to run one or more {@link Ext.data.Operation operations} in a given order. Fires the 'operationcomplete' event
20 * after the completion of each Operation, and the 'complete' event when all Operations have been successfully executed. Fires an 'exception'
21 * event if any of the Operations encounter an exception.</p>
23 * <p>Usually these are only used internally by {@link Ext.data.proxy.Proxy} classes</p>
26 Ext.define('Ext.data.Batch', {
28 observable: 'Ext.util.Observable'
32 * True to immediately start processing the batch as soon as it is constructed (defaults to false)
39 * The index of the current operation being executed
46 * The total number of operations in this batch. Read only
53 * True if the batch is currently running
60 * True if this batch has been executed completely
61 * @property isComplete
67 * True if this batch has encountered an exception. This is cleared at the start of each operation
68 * @property hasException
74 * True to automatically pause the execution of the batch if any operation encounters an exception (defaults to true)
75 * @property pauseOnException
78 pauseOnException: true,
81 * Creates new Batch object.
82 * @param {Object} config (optional) Config object
84 constructor: function(config) {
90 * Fired when all operations of this batch have been completed
91 * @param {Ext.data.Batch} batch The batch object
92 * @param {Object} operation The last operation that was executed
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
105 * @event operationcomplete
106 * Fired when each operation of the batch completes
107 * @param {Ext.data.Batch} batch The batch object
108 * @param {Object} operation The operation that just completed
113 me.mixins.observable.constructor.call(me, config);
116 * Ordered array of operations that will be executed by this batch
117 * @property operations
124 * Adds a new operation to this batch
125 * @param {Object} operation The {@link Ext.data.Operation Operation} object
127 add: function(operation) {
130 operation.setBatch(this);
132 this.operations.push(operation);
136 * Kicks off the execution of the batch, continuing from the next operation if the previous
137 * operation encountered an exception, or if execution was paused
140 this.hasException = false;
141 this.isRunning = true;
143 this.runNextOperation();
148 * Runs the next operation, relative to this.current.
150 runNextOperation: function() {
151 this.runOperation(this.current + 1);
155 * Pauses execution of the batch, but does not cancel the current operation
158 this.isRunning = false;
162 * Executes a operation by its numeric index
163 * @param {Number} index The operation index to run
165 runOperation: function(index) {
167 operations = me.operations,
168 operation = operations[index],
171 if (operation === undefined) {
172 me.isRunning = false;
173 me.isComplete = true;
174 me.fireEvent('complete', me, operations[operations.length - 1]);
178 onProxyReturn = function(operation) {
179 var hasException = operation.hasException();
182 me.hasException = true;
183 me.fireEvent('exception', me, operation);
185 me.fireEvent('operationcomplete', me, operation);
188 if (hasException && me.pauseOnException) {
191 operation.setCompleted();
192 me.runNextOperation();
196 operation.setStarted();
198 me.proxy[operation.action](operation, onProxyReturn, me);