Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / Batch.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.Batch-method-constructor'><span id='Ext-data.Batch'>/**
2 </span></span> * @author Ed Spencer
3  * @class Ext.data.Batch
4  * 
5  * &lt;p&gt;Provides a mechanism to run one or more {@link Ext.data.Operation operations} in a given order. Fires the 'operationcomplete' event
6  * after the completion of each Operation, and the 'complete' event when all Operations have been successfully executed. Fires an 'exception'
7  * event if any of the Operations encounter an exception.&lt;/p&gt;
8  * 
9  * &lt;p&gt;Usually these are only used internally by {@link Ext.data.proxy.Proxy} classes&lt;/p&gt;
10  * 
11  * @constructor
12  * @param {Object} config Optional config object
13  */
14 Ext.define('Ext.data.Batch', {
15     mixins: {
16         observable: 'Ext.util.Observable'
17     },
18     
19 <span id='Ext-data.Batch-property-autoStart'>    /**
20 </span>     * True to immediately start processing the batch as soon as it is constructed (defaults to false)
21      * @property autoStart
22      * @type Boolean
23      */
24     autoStart: false,
25     
26 <span id='Ext-data.Batch-property-current'>    /**
27 </span>     * The index of the current operation being executed
28      * @property current
29      * @type Number
30      */
31     current: -1,
32     
33 <span id='Ext-data.Batch-property-total'>    /**
34 </span>     * The total number of operations in this batch. Read only
35      * @property total
36      * @type Number
37      */
38     total: 0,
39     
40 <span id='Ext-data.Batch-property-isRunning'>    /**
41 </span>     * True if the batch is currently running
42      * @property isRunning
43      * @type Boolean
44      */
45     isRunning: false,
46     
47 <span id='Ext-data.Batch-property-isComplete'>    /**
48 </span>     * True if this batch has been executed completely
49      * @property isComplete
50      * @type Boolean
51      */
52     isComplete: false,
53     
54 <span id='Ext-data.Batch-property-hasException'>    /**
55 </span>     * True if this batch has encountered an exception. This is cleared at the start of each operation
56      * @property hasException
57      * @type Boolean
58      */
59     hasException: false,
60     
61 <span id='Ext-data.Batch-property-pauseOnException'>    /**
62 </span>     * True to automatically pause the execution of the batch if any operation encounters an exception (defaults to true)
63      * @property pauseOnException
64      * @type Boolean
65      */
66     pauseOnException: true,
67     
68     constructor: function(config) {   
69         var me = this;
70                      
71         me.addEvents(
72 <span id='Ext-data.Batch-event-complete'>          /**
73 </span>           * @event complete
74            * Fired when all operations of this batch have been completed
75            * @param {Ext.data.Batch} batch The batch object
76            * @param {Object} operation The last operation that was executed
77            */
78           'complete',
79           
80 <span id='Ext-data.Batch-event-exception'>          /**
81 </span>           * @event exception
82            * Fired when a operation encountered an exception
83            * @param {Ext.data.Batch} batch The batch object
84            * @param {Object} operation The operation that encountered the exception
85            */
86           'exception',
87           
88 <span id='Ext-data.Batch-event-operationcomplete'>          /**
89 </span>           * @event operationcomplete
90            * Fired when each operation of the batch completes
91            * @param {Ext.data.Batch} batch The batch object
92            * @param {Object} operation The operation that just completed
93            */
94           'operationcomplete'
95         );
96         
97         me.mixins.observable.constructor.call(me, config);
98         
99 <span id='Ext-data.Batch-property-operations'>        /**
100 </span>         * Ordered array of operations that will be executed by this batch
101          * @property operations
102          * @type Array
103          */
104         me.operations = [];
105     },
106     
107 <span id='Ext-data.Batch-method-add'>    /**
108 </span>     * Adds a new operation to this batch
109      * @param {Object} operation The {@link Ext.data.Operation Operation} object
110      */
111     add: function(operation) {
112         this.total++;
113         
114         operation.setBatch(this);
115         
116         this.operations.push(operation);
117     },
118     
119 <span id='Ext-data.Batch-method-start'>    /**
120 </span>     * Kicks off the execution of the batch, continuing from the next operation if the previous
121      * operation encountered an exception, or if execution was paused
122      */
123     start: function() {
124         this.hasException = false;
125         this.isRunning = true;
126         
127         this.runNextOperation();
128     },
129     
130 <span id='Ext-data.Batch-method-runNextOperation'>    /**
131 </span>     * @private
132      * Runs the next operation, relative to this.current.
133      */
134     runNextOperation: function() {
135         this.runOperation(this.current + 1);
136     },
137     
138 <span id='Ext-data.Batch-method-pause'>    /**
139 </span>     * Pauses execution of the batch, but does not cancel the current operation
140      */
141     pause: function() {
142         this.isRunning = false;
143     },
144     
145 <span id='Ext-data.Batch-method-runOperation'>    /**
146 </span>     * Executes a operation by its numeric index
147      * @param {Number} index The operation index to run
148      */
149     runOperation: function(index) {
150         var me = this,
151             operations = me.operations,
152             operation  = operations[index],
153             onProxyReturn;
154         
155         if (operation === undefined) {
156             me.isRunning  = false;
157             me.isComplete = true;
158             me.fireEvent('complete', me, operations[operations.length - 1]);
159         } else {
160             me.current = index;
161             
162             onProxyReturn = function(operation) {
163                 var hasException = operation.hasException();
164                 
165                 if (hasException) {
166                     me.hasException = true;
167                     me.fireEvent('exception', me, operation);
168                 } else {
169                     me.fireEvent('operationcomplete', me, operation);
170                 }
171
172                 if (hasException &amp;&amp; me.pauseOnException) {
173                     me.pause();
174                 } else {
175                     operation.setCompleted();
176                     me.runNextOperation();
177                 }
178             };
179             
180             operation.setStarted();
181             
182             me.proxy[operation.action](operation, onProxyReturn, me);
183         }
184     }
185 });</pre></pre></body></html>