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 * @property {Boolean} autoStart
33 * True to immediately start processing the batch as soon as it is constructed.
38 * @property {Number} current
39 * The index of the current operation being executed
44 * @property {Number} total
45 * The total number of operations in this batch. Read only
50 * @property {Boolean} isRunning
51 * True if the batch is currently running
56 * @property {Boolean} isComplete
57 * True if this batch has been executed completely
62 * @property {Boolean} hasException
63 * True if this batch has encountered an exception. This is cleared at the start of each operation
68 * @property {Boolean} pauseOnException
69 * True to automatically pause the execution of the batch if any operation encounters an exception
71 pauseOnException: true,
74 * Creates new Batch object.
75 * @param {Object} [config] Config object
77 constructor: function(config) {
83 * Fired when all operations of this batch have been completed
84 * @param {Ext.data.Batch} batch The batch object
85 * @param {Object} operation The last operation that was executed
91 * Fired when a operation encountered an exception
92 * @param {Ext.data.Batch} batch The batch object
93 * @param {Object} operation The operation that encountered the exception
98 * @event operationcomplete
99 * Fired when each operation of the batch completes
100 * @param {Ext.data.Batch} batch The batch object
101 * @param {Object} operation The operation that just completed
106 me.mixins.observable.constructor.call(me, config);
109 * Ordered array of operations that will be executed by this batch
110 * @property {Ext.data.Operation[]} operations
116 * Adds a new operation to this batch
117 * @param {Object} operation The {@link Ext.data.Operation Operation} object
119 add: function(operation) {
122 operation.setBatch(this);
124 this.operations.push(operation);
128 * Kicks off the execution of the batch, continuing from the next operation if the previous
129 * operation encountered an exception, or if execution was paused
132 this.hasException = false;
133 this.isRunning = true;
135 this.runNextOperation();
140 * Runs the next operation, relative to this.current.
142 runNextOperation: function() {
143 this.runOperation(this.current + 1);
147 * Pauses execution of the batch, but does not cancel the current operation
150 this.isRunning = false;
154 * Executes a operation by its numeric index
155 * @param {Number} index The operation index to run
157 runOperation: function(index) {
159 operations = me.operations,
160 operation = operations[index],
163 if (operation === undefined) {
164 me.isRunning = false;
165 me.isComplete = true;
166 me.fireEvent('complete', me, operations[operations.length - 1]);
170 onProxyReturn = function(operation) {
171 var hasException = operation.hasException();
174 me.hasException = true;
175 me.fireEvent('exception', me, operation);
177 me.fireEvent('operationcomplete', me, operation);
180 if (hasException && me.pauseOnException) {
183 operation.setCompleted();
184 me.runNextOperation();
188 operation.setStarted();
190 me.proxy[operation.action](operation, onProxyReturn, me);