Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Batch.html
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="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../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-method-constructor'><span id='Ext-data-Batch'>/**
19 </span></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  * @constructor
29  * @param {Object} config Optional config object
30  */
31 Ext.define('Ext.data.Batch', {
32     mixins: {
33         observable: 'Ext.util.Observable'
34     },
35     
36 <span id='Ext-data-Batch-property-autoStart'>    /**
37 </span>     * True to immediately start processing the batch as soon as it is constructed (defaults to false)
38      * @property autoStart
39      * @type Boolean
40      */
41     autoStart: false,
42     
43 <span id='Ext-data-Batch-property-current'>    /**
44 </span>     * The index of the current operation being executed
45      * @property current
46      * @type Number
47      */
48     current: -1,
49     
50 <span id='Ext-data-Batch-property-total'>    /**
51 </span>     * The total number of operations in this batch. Read only
52      * @property total
53      * @type Number
54      */
55     total: 0,
56     
57 <span id='Ext-data-Batch-property-isRunning'>    /**
58 </span>     * True if the batch is currently running
59      * @property isRunning
60      * @type Boolean
61      */
62     isRunning: false,
63     
64 <span id='Ext-data-Batch-property-isComplete'>    /**
65 </span>     * True if this batch has been executed completely
66      * @property isComplete
67      * @type Boolean
68      */
69     isComplete: false,
70     
71 <span id='Ext-data-Batch-property-hasException'>    /**
72 </span>     * True if this batch has encountered an exception. This is cleared at the start of each operation
73      * @property hasException
74      * @type Boolean
75      */
76     hasException: false,
77     
78 <span id='Ext-data-Batch-property-pauseOnException'>    /**
79 </span>     * True to automatically pause the execution of the batch if any operation encounters an exception (defaults to true)
80      * @property pauseOnException
81      * @type Boolean
82      */
83     pauseOnException: true,
84     
85     constructor: function(config) {   
86         var me = this;
87                      
88         me.addEvents(
89 <span id='Ext-data-Batch-event-complete'>          /**
90 </span>           * @event complete
91            * Fired when all operations of this batch have been completed
92            * @param {Ext.data.Batch} batch The batch object
93            * @param {Object} operation The last operation that was executed
94            */
95           'complete',
96           
97 <span id='Ext-data-Batch-event-exception'>          /**
98 </span>           * @event exception
99            * Fired when a operation encountered an exception
100            * @param {Ext.data.Batch} batch The batch object
101            * @param {Object} operation The operation that encountered the exception
102            */
103           'exception',
104           
105 <span id='Ext-data-Batch-event-operationcomplete'>          /**
106 </span>           * @event operationcomplete
107            * Fired when each operation of the batch completes
108            * @param {Ext.data.Batch} batch The batch object
109            * @param {Object} operation The operation that just completed
110            */
111           'operationcomplete'
112         );
113         
114         me.mixins.observable.constructor.call(me, config);
115         
116 <span id='Ext-data-Batch-property-operations'>        /**
117 </span>         * Ordered array of operations that will be executed by this batch
118          * @property operations
119          * @type Array
120          */
121         me.operations = [];
122     },
123     
124 <span id='Ext-data-Batch-method-add'>    /**
125 </span>     * Adds a new operation to this batch
126      * @param {Object} operation The {@link Ext.data.Operation Operation} object
127      */
128     add: function(operation) {
129         this.total++;
130         
131         operation.setBatch(this);
132         
133         this.operations.push(operation);
134     },
135     
136 <span id='Ext-data-Batch-method-start'>    /**
137 </span>     * Kicks off the execution of the batch, continuing from the next operation if the previous
138      * operation encountered an exception, or if execution was paused
139      */
140     start: function() {
141         this.hasException = false;
142         this.isRunning = true;
143         
144         this.runNextOperation();
145     },
146     
147 <span id='Ext-data-Batch-method-runNextOperation'>    /**
148 </span>     * @private
149      * Runs the next operation, relative to this.current.
150      */
151     runNextOperation: function() {
152         this.runOperation(this.current + 1);
153     },
154     
155 <span id='Ext-data-Batch-method-pause'>    /**
156 </span>     * Pauses execution of the batch, but does not cancel the current operation
157      */
158     pause: function() {
159         this.isRunning = false;
160     },
161     
162 <span id='Ext-data-Batch-method-runOperation'>    /**
163 </span>     * Executes a operation by its numeric index
164      * @param {Number} index The operation index to run
165      */
166     runOperation: function(index) {
167         var me = this,
168             operations = me.operations,
169             operation  = operations[index],
170             onProxyReturn;
171         
172         if (operation === undefined) {
173             me.isRunning  = false;
174             me.isComplete = true;
175             me.fireEvent('complete', me, operations[operations.length - 1]);
176         } else {
177             me.current = index;
178             
179             onProxyReturn = function(operation) {
180                 var hasException = operation.hasException();
181                 
182                 if (hasException) {
183                     me.hasException = true;
184                     me.fireEvent('exception', me, operation);
185                 } else {
186                     me.fireEvent('operationcomplete', me, operation);
187                 }
188
189                 if (hasException &amp;&amp; me.pauseOnException) {
190                     me.pause();
191                 } else {
192                     operation.setCompleted();
193                     me.runNextOperation();
194                 }
195             };
196             
197             operation.setStarted();
198             
199             me.proxy[operation.action](operation, onProxyReturn, me);
200         }
201     }
202 });</pre>
203 </body>
204 </html>