Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Action2.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-form-action-Action-method-constructor'><span id='Ext-form-action-Action'>/**
19 </span></span> * @class Ext.form.action.Action
20  * @extends Ext.Base
21  * &lt;p&gt;The subclasses of this class provide actions to perform upon {@link Ext.form.Basic Form}s.&lt;/p&gt;
22  * &lt;p&gt;Instances of this class are only created by a {@link Ext.form.Basic Form} when
23  * the Form needs to perform an action such as submit or load. The Configuration options
24  * listed for this class are set through the Form's action methods: {@link Ext.form.Basic#submit submit},
25  * {@link Ext.form.Basic#load load} and {@link Ext.form.Basic#doAction doAction}&lt;/p&gt;
26  * &lt;p&gt;The instance of Action which performed the action is passed to the success
27  * and failure callbacks of the Form's action methods ({@link Ext.form.Basic#submit submit},
28  * {@link Ext.form.Basic#load load} and {@link Ext.form.Basic#doAction doAction}),
29  * and to the {@link Ext.form.Basic#actioncomplete actioncomplete} and
30  * {@link Ext.form.Basic#actionfailed actionfailed} event handlers.&lt;/p&gt;
31  * @constructor
32  * @param {Object} config The configuration for this instance.
33  */
34 Ext.define('Ext.form.action.Action', {
35     alternateClassName: 'Ext.form.Action',
36
37 <span id='Ext-form-action-Action-cfg-form'>    /**
38 </span>     * @cfg {Ext.form.Basic} form The {@link Ext.form.Basic BasicForm} instance that
39      * is invoking this Action. Required.
40      */
41
42 <span id='Ext-form-action-Action-cfg-url'>    /**
43 </span>     * @cfg {String} url The URL that the Action is to invoke. Will default to the {@link Ext.form.Basic#url url}
44      * configured on the {@link #form}.
45      */
46
47 <span id='Ext-form-action-Action-cfg-reset'>    /**
48 </span>     * @cfg {Boolean} reset When set to &lt;tt&gt;&lt;b&gt;true&lt;/b&gt;&lt;/tt&gt;, causes the Form to be
49      * {@link Ext.form.Basic#reset reset} on Action success. If specified, this happens
50      * before the {@link #success} callback is called and before the Form's
51      * {@link Ext.form.Basic#actioncomplete actioncomplete} event fires.
52      */
53
54 <span id='Ext-form-action-Action-cfg-method'>    /**
55 </span>     * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
56      * {@link Ext.form.Basic#method BasicForm's method}, or 'POST' if not specified.
57      */
58
59 <span id='Ext-form-action-Action-cfg-params'>    /**
60 </span>     * @cfg {Object/String} params &lt;p&gt;Extra parameter values to pass. These are added to the Form's
61      * {@link Ext.form.Basic#baseParams} and passed to the specified URL along with the Form's
62      * input fields.&lt;/p&gt;
63      * &lt;p&gt;Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode Ext.Object.toQueryString}.&lt;/p&gt;
64      */
65
66 <span id='Ext-form-action-Action-cfg-headers'>    /**
67 </span>     * @cfg {Object} headers &lt;p&gt;Extra headers to be sent in the AJAX request for submit and load actions. See
68      * {@link Ext.data.Connection#headers}.&lt;/p&gt;
69      */
70
71 <span id='Ext-form-action-Action-cfg-timeout'>    /**
72 </span>     * @cfg {Number} timeout The number of seconds to wait for a server response before
73      * failing with the {@link #failureType} as {@link Ext.form.action.Action#CONNECT_FAILURE}. If not specified,
74      * defaults to the configured &lt;tt&gt;{@link Ext.form.Basic#timeout timeout}&lt;/tt&gt; of the
75      * {@link #form}.
76      */
77
78 <span id='Ext-form-action-Action-cfg-success'>    /**
79 </span>     * @cfg {Function} success The function to call when a valid success return packet is received.
80      * The function is passed the following parameters:&lt;ul class=&quot;mdetail-params&quot;&gt;
81      * &lt;li&gt;&lt;b&gt;form&lt;/b&gt; : Ext.form.Basic&lt;div class=&quot;sub-desc&quot;&gt;The form that requested the action&lt;/div&gt;&lt;/li&gt;
82      * &lt;li&gt;&lt;b&gt;action&lt;/b&gt; : Ext.form.action.Action&lt;div class=&quot;sub-desc&quot;&gt;The Action class. The {@link #result}
83      * property of this object may be examined to perform custom postprocessing.&lt;/div&gt;&lt;/li&gt;
84      * &lt;/ul&gt;
85      */
86
87 <span id='Ext-form-action-Action-cfg-failure'>    /**
88 </span>     * @cfg {Function} failure The function to call when a failure packet was received, or when an
89      * error ocurred in the Ajax communication.
90      * The function is passed the following parameters:&lt;ul class=&quot;mdetail-params&quot;&gt;
91      * &lt;li&gt;&lt;b&gt;form&lt;/b&gt; : Ext.form.Basic&lt;div class=&quot;sub-desc&quot;&gt;The form that requested the action&lt;/div&gt;&lt;/li&gt;
92      * &lt;li&gt;&lt;b&gt;action&lt;/b&gt; : Ext.form.action.Action&lt;div class=&quot;sub-desc&quot;&gt;The Action class. If an Ajax
93      * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
94      * property of this object may be examined to perform custom postprocessing.&lt;/div&gt;&lt;/li&gt;
95      * &lt;/ul&gt;
96      */
97
98 <span id='Ext-form-action-Action-cfg-scope'>    /**
99 </span>     * @cfg {Object} scope The scope in which to call the configured &lt;tt&gt;success&lt;/tt&gt; and &lt;tt&gt;failure&lt;/tt&gt;
100      * callback functions (the &lt;tt&gt;this&lt;/tt&gt; reference for the callback functions).
101      */
102
103 <span id='Ext-form-action-Action-cfg-waitMsg'>    /**
104 </span>     * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.window.MessageBox#wait}
105      * during the time the action is being processed.
106      */
107
108 <span id='Ext-form-action-Action-cfg-waitTitle'>    /**
109 </span>     * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.window.MessageBox#wait}
110      * during the time the action is being processed.
111      */
112
113 <span id='Ext-form-action-Action-cfg-submitEmptyText'>    /**
114 </span>     * @cfg {Boolean} submitEmptyText If set to &lt;tt&gt;true&lt;/tt&gt;, the emptyText value will be sent with the form
115      * when it is submitted. Defaults to &lt;tt&gt;true&lt;/tt&gt;.
116      */
117
118 <span id='Ext-form-action-Action-property-type'>    /**
119 </span>     * @property type
120      * The type of action this Action instance performs.
121      * Currently only &quot;submit&quot; and &quot;load&quot; are supported.
122      * @type {String}
123      */
124
125 <span id='Ext-form-action-Action-property-failureType'>    /**
126 </span>     * The type of failure detected will be one of these: {@link Ext.form.action.Action#CLIENT_INVALID},
127      * {@link Ext.form.action.Action#SERVER_INVALID}, {@link Ext.form.action.Action#CONNECT_FAILURE}, or
128      * {@link Ext.form.action.Action#LOAD_FAILURE}.  Usage:
129      * &lt;pre&gt;&lt;code&gt;
130 var fp = new Ext.form.Panel({
131 ...
132 buttons: [{
133     text: 'Save',
134     formBind: true,
135     handler: function(){
136         if(fp.getForm().isValid()){
137             fp.getForm().submit({
138                 url: 'form-submit.php',
139                 waitMsg: 'Submitting your data...',
140                 success: function(form, action){
141                     // server responded with success = true
142                     var result = action.{@link #result};
143                 },
144                 failure: function(form, action){
145                     if (action.{@link #failureType} === {@link Ext.form.action.Action#CONNECT_FAILURE}) {
146                         Ext.Msg.alert('Error',
147                             'Status:'+action.{@link #response}.status+': '+
148                             action.{@link #response}.statusText);
149                     }
150                     if (action.failureType === {@link Ext.form.action.Action#SERVER_INVALID}){
151                         // server responded with success = false
152                         Ext.Msg.alert('Invalid', action.{@link #result}.errormsg);
153                     }
154                 }
155             });
156         }
157     }
158 },{
159     text: 'Reset',
160     handler: function(){
161         fp.getForm().reset();
162     }
163 }]
164      * &lt;/code&gt;&lt;/pre&gt;
165      * @property failureType
166      * @type {String}
167      */
168
169 <span id='Ext-form-action-Action-property-response'>    /**
170 </span>     * The raw XMLHttpRequest object used to perform the action.
171      * @property response
172      * @type {Object}
173      */
174
175 <span id='Ext-form-action-Action-property-result'>    /**
176 </span>     * The decoded response object containing a boolean &lt;tt&gt;success&lt;/tt&gt; property and
177      * other, action-specific properties.
178      * @property result
179      * @type {Object}
180      */
181
182
183
184     constructor: function(config) {
185         if (config) {
186             Ext.apply(this, config);
187         }
188
189         // Normalize the params option to an Object
190         var params = config.params;
191         if (Ext.isString(params)) {
192             this.params = Ext.Object.fromQueryString(params);
193         }
194     },
195
196 <span id='Ext-form-action-Action-property-run'>    /**
197 </span>     * Invokes this action using the current configuration.
198      */
199     run: Ext.emptyFn,
200
201 <span id='Ext-form-action-Action-method-onSuccess'>    /**
202 </span>     * @private
203      * @method onSuccess
204      * Callback method that gets invoked when the action completes successfully. Must be implemented by subclasses.
205      * @param {Object} response
206      */
207
208 <span id='Ext-form-action-Action-method-handleResponse'>    /**
209 </span>     * @private
210      * @method handleResponse
211      * Handles the raw response and builds a result object from it. Must be implemented by subclasses.
212      * @param {Object} response
213      */
214
215 <span id='Ext-form-action-Action-method-onFailure'>    /**
216 </span>     * @private
217      * Handles a failure response.
218      * @param {Object} response
219      */
220     onFailure : function(response){
221         this.response = response;
222         this.failureType = Ext.form.action.Action.CONNECT_FAILURE;
223         this.form.afterAction(this, false);
224     },
225
226 <span id='Ext-form-action-Action-method-processResponse'>    /**
227 </span>     * @private
228      * Validates that a response contains either responseText or responseXML and invokes
229      * {@link #handleResponse} to build the result object.
230      * @param {Object} response The raw response object.
231      * @return {Object/Boolean} result The result object as built by handleResponse, or &lt;tt&gt;true&lt;/tt&gt; if
232      *                         the response had empty responseText and responseXML.
233      */
234     processResponse : function(response){
235         this.response = response;
236         if (!response.responseText &amp;&amp; !response.responseXML) {
237             return true;
238         }
239         return (this.result = this.handleResponse(response));
240     },
241
242 <span id='Ext-form-action-Action-method-getUrl'>    /**
243 </span>     * @private
244      * Build the URL for the AJAX request. Used by the standard AJAX submit and load actions.
245      * @return {String} The URL.
246      */
247     getUrl: function() {
248         return this.url || this.form.url;
249     },
250
251 <span id='Ext-form-action-Action-method-getMethod'>    /**
252 </span>     * @private
253      * Determine the HTTP method to be used for the request.
254      * @return {String} The HTTP method
255      */
256     getMethod: function() {
257         return (this.method || this.form.method || 'POST').toUpperCase();
258     },
259
260 <span id='Ext-form-action-Action-method-getParams'>    /**
261 </span>     * @private
262      * Get the set of parameters specified in the BasicForm's baseParams and/or the params option.
263      * Items in params override items of the same name in baseParams.
264      * @return {Object} the full set of parameters
265      */
266     getParams: function() {
267         return Ext.apply({}, this.params, this.form.baseParams);
268     },
269
270 <span id='Ext-form-action-Action-method-createCallback'>    /**
271 </span>     * @private
272      * Creates a callback object.
273      */
274     createCallback: function() {
275         var me = this,
276             undef,
277             form = me.form;
278         return {
279             success: me.onSuccess,
280             failure: me.onFailure,
281             scope: me,
282             timeout: (this.timeout * 1000) || (form.timeout * 1000),
283             upload: form.fileUpload ? me.onSuccess : undef
284         };
285     },
286
287     statics: {
288 <span id='Ext-form-action-Action-property-CLIENT_INVALID'>        /**
289 </span>         * @property CLIENT_INVALID
290          * Failure type returned when client side validation of the Form fails
291          * thus aborting a submit action. Client side validation is performed unless
292          * {@link Ext.form.action.Submit#clientValidation} is explicitly set to &lt;tt&gt;false&lt;/tt&gt;.
293          * @type {String}
294          * @static
295          */
296         CLIENT_INVALID: 'client',
297
298 <span id='Ext-form-action-Action-property-SERVER_INVALID'>        /**
299 </span>         * @property SERVER_INVALID
300          * &lt;p&gt;Failure type returned when server side processing fails and the {@link #result}'s
301          * &lt;tt&gt;success&lt;/tt&gt; property is set to &lt;tt&gt;false&lt;/tt&gt;.&lt;/p&gt;
302          * &lt;p&gt;In the case of a form submission, field-specific error messages may be returned in the
303          * {@link #result}'s &lt;tt&gt;errors&lt;/tt&gt; property.&lt;/p&gt;
304          * @type {String}
305          * @static
306          */
307         SERVER_INVALID: 'server',
308
309 <span id='Ext-form-action-Action-property-CONNECT_FAILURE'>        /**
310 </span>         * @property CONNECT_FAILURE
311          * Failure type returned when a communication error happens when attempting
312          * to send a request to the remote server. The {@link #response} may be examined to
313          * provide further information.
314          * @type {String}
315          * @static
316          */
317         CONNECT_FAILURE: 'connect',
318
319 <span id='Ext-form-action-Action-property-LOAD_FAILURE'>        /**
320 </span>         * @property LOAD_FAILURE
321          * Failure type returned when the response's &lt;tt&gt;success&lt;/tt&gt;
322          * property is set to &lt;tt&gt;false&lt;/tt&gt;, or no field values are returned in the response's
323          * &lt;tt&gt;data&lt;/tt&gt; property.
324          * @type {String}
325          * @static
326          */
327         LOAD_FAILURE: 'load'
328
329
330     }
331 });
332 </pre>
333 </body>
334 </html>