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