Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Action.html
index f9a2232..e2a67a9 100644 (file)
   </script>
 </head>
 <body onload="prettyPrint(); highlight();">
-  <pre class="prettyprint lang-js"><span id='Ext-Action-method-constructor'><span id='Ext-Action'>/**
-</span></span> * @class Ext.Action
- * &lt;p&gt;An Action is a piece of reusable functionality that can be abstracted out of any particular component so that it
- * can be usefully shared among multiple components.  Actions let you share handlers, configuration options and UI
- * updates across any components that support the Action interface (primarily {@link Ext.toolbar.Toolbar}, {@link Ext.button.Button}
- * and {@link Ext.menu.Menu} components).&lt;/p&gt;
- * &lt;p&gt;Use a single Action instance as the config object for any number of UI Components which share the same configuration. The
- * Action not only supplies the configuration, but allows all Components based upon it to have a common set of methods
- * called at once through a single call to the Action.&lt;/p&gt;
- * &lt;p&gt;Any Component that is to be configured with an Action must also support
- * the following methods:&lt;ul&gt;
- * &lt;li&gt;&lt;code&gt;setText(string)&lt;/code&gt;&lt;/li&gt;
- * &lt;li&gt;&lt;code&gt;setIconCls(string)&lt;/code&gt;&lt;/li&gt;
- * &lt;li&gt;&lt;code&gt;setDisabled(boolean)&lt;/code&gt;&lt;/li&gt;
- * &lt;li&gt;&lt;code&gt;setVisible(boolean)&lt;/code&gt;&lt;/li&gt;
- * &lt;li&gt;&lt;code&gt;setHandler(function)&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;.&lt;/p&gt;
- * &lt;p&gt;This allows the Action to control its associated Components.&lt;/p&gt;
- * Example usage:&lt;br&gt;
- * &lt;pre&gt;&lt;code&gt;
-// Define the shared Action.  Each Component below will have the same
-// display text and icon, and will display the same message on click.
-var action = new Ext.Action({
-    {@link #text}: 'Do something',
-    {@link #handler}: function(){
-        Ext.Msg.alert('Click', 'You did something.');
-    },
-    {@link #iconCls}: 'do-something',
-    {@link #itemId}: 'myAction'
-});
-
-var panel = new Ext.panel.Panel({
-    title: 'Actions',
-    width: 500,
-    height: 300,
-    tbar: [
-        // Add the Action directly to a toolbar as a menu button
-        action,
-        {
-            text: 'Action Menu',
-            // Add the Action to a menu as a text item
-            menu: [action]
-        }
-    ],
-    items: [
-        // Add the Action to the panel body as a standard button
-        new Ext.button.Button(action)
-    ],
-    renderTo: Ext.getBody()
-});
+  <pre class="prettyprint lang-js"><span id='Ext-form-action-Action'>/**
+</span> * @class Ext.form.action.Action
+ * @extends Ext.Base
+ * &lt;p&gt;The subclasses of this class provide actions to perform upon {@link Ext.form.Basic Form}s.&lt;/p&gt;
+ * &lt;p&gt;Instances of this class are only created by a {@link Ext.form.Basic Form} when
+ * the Form needs to perform an action such as submit or load. The Configuration options
+ * listed for this class are set through the Form's action methods: {@link Ext.form.Basic#submit submit},
+ * {@link Ext.form.Basic#load load} and {@link Ext.form.Basic#doAction doAction}&lt;/p&gt;
+ * &lt;p&gt;The instance of Action which performed the action is passed to the success
+ * and failure callbacks of the Form's action methods ({@link Ext.form.Basic#submit submit},
+ * {@link Ext.form.Basic#load load} and {@link Ext.form.Basic#doAction doAction}),
+ * and to the {@link Ext.form.Basic#actioncomplete actioncomplete} and
+ * {@link Ext.form.Basic#actionfailed actionfailed} event handlers.&lt;/p&gt;
+ */
+Ext.define('Ext.form.action.Action', {
+    alternateClassName: 'Ext.form.Action',
 
-// Change the text for all components using the Action
-action.setText('Something else');
+<span id='Ext-form-action-Action-cfg-form'>    /**
+</span>     * @cfg {Ext.form.Basic} form The {@link Ext.form.Basic BasicForm} instance that
+     * is invoking this Action. Required.
+     */
 
-// Reference an Action through a container using the itemId
-var btn = panel.getComponent('myAction');
-var aRef = btn.baseAction;
-aRef.setText('New text');
-&lt;/code&gt;&lt;/pre&gt;
- * @constructor
- * @param {Object} config The configuration options
- */
-Ext.define('Ext.Action', {
+<span id='Ext-form-action-Action-cfg-url'>    /**
+</span>     * @cfg {String} url The URL that the Action is to invoke. Will default to the {@link Ext.form.Basic#url url}
+     * configured on the {@link #form}.
+     */
 
-    /* Begin Definitions */
+<span id='Ext-form-action-Action-cfg-reset'>    /**
+</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
+     * {@link Ext.form.Basic#reset reset} on Action success. If specified, this happens
+     * before the {@link #success} callback is called and before the Form's
+     * {@link Ext.form.Basic#actioncomplete actioncomplete} event fires.
+     */
 
-    /* End Definitions */
+<span id='Ext-form-action-Action-cfg-method'>    /**
+</span>     * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
+     * {@link Ext.form.Basic#method BasicForm's method}, or 'POST' if not specified.
+     */
 
-<span id='Ext-Action-cfg-text'>    /**
-</span>     * @cfg {String} text The text to set for all components configured by this Action (defaults to '').
+<span id='Ext-form-action-Action-cfg-params'>    /**
+</span>     * @cfg {Object/String} params &lt;p&gt;Extra parameter values to pass. These are added to the Form's
+     * {@link Ext.form.Basic#baseParams} and passed to the specified URL along with the Form's
+     * input fields.&lt;/p&gt;
+     * &lt;p&gt;Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode Ext.Object.toQueryString}.&lt;/p&gt;
      */
-<span id='Ext-Action-cfg-iconCls'>    /**
-</span>     * @cfg {String} iconCls
-     * The CSS class selector that specifies a background image to be used as the header icon for
-     * all components configured by this Action (defaults to '').
-     * &lt;p&gt;An example of specifying a custom icon class would be something like:
-     * &lt;/p&gt;&lt;pre&gt;&lt;code&gt;
-// specify the property in the config for the class:
-     ...
-     iconCls: 'do-something'
-
-// css class that specifies background image to be used as the icon image:
-.do-something { background-image: url(../images/my-icon.gif) 0 6px no-repeat !important; }
-&lt;/code&gt;&lt;/pre&gt;
+
+<span id='Ext-form-action-Action-cfg-headers'>    /**
+</span>     * @cfg {Object} headers &lt;p&gt;Extra headers to be sent in the AJAX request for submit and load actions. See
+     * {@link Ext.data.Connection#headers}.&lt;/p&gt;
      */
-<span id='Ext-Action-cfg-disabled'>    /**
-</span>     * @cfg {Boolean} disabled True to disable all components configured by this Action, false to enable them (defaults to false).
+
+<span id='Ext-form-action-Action-cfg-timeout'>    /**
+</span>     * @cfg {Number} timeout The number of seconds to wait for a server response before
+     * failing with the {@link #failureType} as {@link Ext.form.action.Action#CONNECT_FAILURE}. If not specified,
+     * defaults to the configured &lt;tt&gt;{@link Ext.form.Basic#timeout timeout}&lt;/tt&gt; of the
+     * {@link #form}.
      */
-<span id='Ext-Action-cfg-hidden'>    /**
-</span>     * @cfg {Boolean} hidden True to hide all components configured by this Action, false to show them (defaults to false).
+
+<span id='Ext-form-action-Action-cfg-success'>    /**
+</span>     * @cfg {Function} success The function to call when a valid success return packet is received.
+     * The function is passed the following parameters:&lt;ul class=&quot;mdetail-params&quot;&gt;
+     * &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;
+     * &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}
+     * property of this object may be examined to perform custom postprocessing.&lt;/div&gt;&lt;/li&gt;
+     * &lt;/ul&gt;
      */
-<span id='Ext-Action-cfg-handler'>    /**
-</span>     * @cfg {Function} handler The function that will be invoked by each component tied to this Action
-     * when the component's primary event is triggered (defaults to undefined).
+
+<span id='Ext-form-action-Action-cfg-failure'>    /**
+</span>     * @cfg {Function} failure The function to call when a failure packet was received, or when an
+     * error ocurred in the Ajax communication.
+     * The function is passed the following parameters:&lt;ul class=&quot;mdetail-params&quot;&gt;
+     * &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;
+     * &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
+     * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
+     * property of this object may be examined to perform custom postprocessing.&lt;/div&gt;&lt;/li&gt;
+     * &lt;/ul&gt;
      */
-<span id='Ext-Action-cfg-itemId'>    /**
-</span>     * @cfg {String} itemId
-     * See {@link Ext.Component}.{@link Ext.Component#itemId itemId}.
+
+<span id='Ext-form-action-Action-cfg-scope'>    /**
+</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;
+     * callback functions (the &lt;tt&gt;this&lt;/tt&gt; reference for the callback functions).
      */
-<span id='Ext-Action-cfg-scope'>    /**
-</span>     * @cfg {Object} scope The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the
-     * &lt;code&gt;{@link #handler}&lt;/code&gt; is executed. Defaults to the browser window.
+
+<span id='Ext-form-action-Action-cfg-waitMsg'>    /**
+</span>     * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.window.MessageBox#wait}
+     * during the time the action is being processed.
      */
 
-    constructor : function(config){
-        this.initialConfig = config;
-        this.itemId = config.itemId = (config.itemId || config.id || Ext.id());
-        this.items = [];
-    },
+<span id='Ext-form-action-Action-cfg-waitTitle'>    /**
+</span>     * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.window.MessageBox#wait}
+     * during the time the action is being processed.
+     */
 
-    // private
-    isAction : true,
+<span id='Ext-form-action-Action-cfg-submitEmptyText'>    /**
+</span>     * @cfg {Boolean} submitEmptyText If set to &lt;tt&gt;true&lt;/tt&gt;, the emptyText value will be sent with the form
+     * when it is submitted. Defaults to &lt;tt&gt;true&lt;/tt&gt;.
+     */
 
-<span id='Ext-Action-method-setText'>    /**
-</span>     * Sets the text to be displayed by all components configured by this Action.
-     * @param {String} text The text to display
+<span id='Ext-form-action-Action-property-type'>    /**
+</span>     * @property type
+     * The type of action this Action instance performs.
+     * Currently only &quot;submit&quot; and &quot;load&quot; are supported.
+     * @type {String}
      */
-    setText : function(text){
-        this.initialConfig.text = text;
-        this.callEach('setText', [text]);
-    },
 
-<span id='Ext-Action-method-getText'>    /**
-</span>     * Gets the text currently displayed by all components configured by this Action.
+<span id='Ext-form-action-Action-property-failureType'>    /**
+</span>     * The type of failure detected will be one of these: {@link Ext.form.action.Action#CLIENT_INVALID},
+     * {@link Ext.form.action.Action#SERVER_INVALID}, {@link Ext.form.action.Action#CONNECT_FAILURE}, or
+     * {@link Ext.form.action.Action#LOAD_FAILURE}.  Usage:
+     * &lt;pre&gt;&lt;code&gt;
+var fp = new Ext.form.Panel({
+...
+buttons: [{
+    text: 'Save',
+    formBind: true,
+    handler: function(){
+        if(fp.getForm().isValid()){
+            fp.getForm().submit({
+                url: 'form-submit.php',
+                waitMsg: 'Submitting your data...',
+                success: function(form, action){
+                    // server responded with success = true
+                    var result = action.{@link #result};
+                },
+                failure: function(form, action){
+                    if (action.{@link #failureType} === {@link Ext.form.action.Action#CONNECT_FAILURE}) {
+                        Ext.Msg.alert('Error',
+                            'Status:'+action.{@link #response}.status+': '+
+                            action.{@link #response}.statusText);
+                    }
+                    if (action.failureType === {@link Ext.form.action.Action#SERVER_INVALID}){
+                        // server responded with success = false
+                        Ext.Msg.alert('Invalid', action.{@link #result}.errormsg);
+                    }
+                }
+            });
+        }
+    }
+},{
+    text: 'Reset',
+    handler: function(){
+        fp.getForm().reset();
+    }
+}]
+     * &lt;/code&gt;&lt;/pre&gt;
+     * @property failureType
+     * @type {String}
      */
-    getText : function(){
-        return this.initialConfig.text;
-    },
 
-<span id='Ext-Action-method-setIconCls'>    /**
-</span>     * Sets the icon CSS class for all components configured by this Action.  The class should supply
-     * a background image that will be used as the icon image.
-     * @param {String} cls The CSS class supplying the icon image
+<span id='Ext-form-action-Action-property-response'>    /**
+</span>     * The raw XMLHttpRequest object used to perform the action.
+     * @property response
+     * @type {Object}
      */
-    setIconCls : function(cls){
-        this.initialConfig.iconCls = cls;
-        this.callEach('setIconCls', [cls]);
-    },
 
-<span id='Ext-Action-method-getIconCls'>    /**
-</span>     * Gets the icon CSS class currently used by all components configured by this Action.
+<span id='Ext-form-action-Action-property-result'>    /**
+</span>     * The decoded response object containing a boolean &lt;tt&gt;success&lt;/tt&gt; property and
+     * other, action-specific properties.
+     * @property result
+     * @type {Object}
      */
-    getIconCls : function(){
-        return this.initialConfig.iconCls;
-    },
 
-<span id='Ext-Action-method-setDisabled'>    /**
-</span>     * Sets the disabled state of all components configured by this Action.  Shortcut method
-     * for {@link #enable} and {@link #disable}.
-     * @param {Boolean} disabled True to disable the component, false to enable it
+<span id='Ext-form-action-Action-method-constructor'>    /**
+</span>     * Creates new Action.
+     * @param {Object} config (optional) Config object.
      */
-    setDisabled : function(v){
-        this.initialConfig.disabled = v;
-        this.callEach('setDisabled', [v]);
+    constructor: function(config) {
+        if (config) {
+            Ext.apply(this, config);
+        }
+
+        // Normalize the params option to an Object
+        var params = config.params;
+        if (Ext.isString(params)) {
+            this.params = Ext.Object.fromQueryString(params);
+        }
     },
 
-<span id='Ext-Action-method-enable'>    /**
-</span>     * Enables all components configured by this Action.
+<span id='Ext-form-action-Action-property-run'>    /**
+</span>     * Invokes this action using the current configuration.
      */
-    enable : function(){
-        this.setDisabled(false);
-    },
+    run: Ext.emptyFn,
 
-<span id='Ext-Action-method-disable'>    /**
-</span>     * Disables all components configured by this Action.
+<span id='Ext-form-action-Action-method-onSuccess'>    /**
+</span>     * @private
+     * @method onSuccess
+     * Callback method that gets invoked when the action completes successfully. Must be implemented by subclasses.
+     * @param {Object} response
      */
-    disable : function(){
-        this.setDisabled(true);
-    },
 
-<span id='Ext-Action-method-isDisabled'>    /**
-</span>     * Returns true if the components using this Action are currently disabled, else returns false.  
+<span id='Ext-form-action-Action-method-handleResponse'>    /**
+</span>     * @private
+     * @method handleResponse
+     * Handles the raw response and builds a result object from it. Must be implemented by subclasses.
+     * @param {Object} response
      */
-    isDisabled : function(){
-        return this.initialConfig.disabled;
-    },
 
-<span id='Ext-Action-method-setHidden'>    /**
-</span>     * Sets the hidden state of all components configured by this Action.  Shortcut method
-     * for &lt;code&gt;{@link #hide}&lt;/code&gt; and &lt;code&gt;{@link #show}&lt;/code&gt;.
-     * @param {Boolean} hidden True to hide the component, false to show it
+<span id='Ext-form-action-Action-method-onFailure'>    /**
+</span>     * @private
+     * Handles a failure response.
+     * @param {Object} response
      */
-    setHidden : function(v){
-        this.initialConfig.hidden = v;
-        this.callEach('setVisible', [!v]);
+    onFailure : function(response){
+        this.response = response;
+        this.failureType = Ext.form.action.Action.CONNECT_FAILURE;
+        this.form.afterAction(this, false);
     },
 
-<span id='Ext-Action-method-show'>    /**
-</span>     * Shows all components configured by this Action.
+<span id='Ext-form-action-Action-method-processResponse'>    /**
+</span>     * @private
+     * Validates that a response contains either responseText or responseXML and invokes
+     * {@link #handleResponse} to build the result object.
+     * @param {Object} response The raw response object.
+     * @return {Object/Boolean} result The result object as built by handleResponse, or &lt;tt&gt;true&lt;/tt&gt; if
+     *                         the response had empty responseText and responseXML.
      */
-    show : function(){
-        this.setHidden(false);
+    processResponse : function(response){
+        this.response = response;
+        if (!response.responseText &amp;&amp; !response.responseXML) {
+            return true;
+        }
+        return (this.result = this.handleResponse(response));
     },
 
-<span id='Ext-Action-method-hide'>    /**
-</span>     * Hides all components configured by this Action.
+<span id='Ext-form-action-Action-method-getUrl'>    /**
+</span>     * @private
+     * Build the URL for the AJAX request. Used by the standard AJAX submit and load actions.
+     * @return {String} The URL.
      */
-    hide : function(){
-        this.setHidden(true);
+    getUrl: function() {
+        return this.url || this.form.url;
     },
 
-<span id='Ext-Action-method-isHidden'>    /**
-</span>     * Returns true if the components configured by this Action are currently hidden, else returns false.
+<span id='Ext-form-action-Action-method-getMethod'>    /**
+</span>     * @private
+     * Determine the HTTP method to be used for the request.
+     * @return {String} The HTTP method
      */
-    isHidden : function(){
-        return this.initialConfig.hidden;
+    getMethod: function() {
+        return (this.method || this.form.method || 'POST').toUpperCase();
     },
 
-<span id='Ext-Action-method-setHandler'>    /**
-</span>     * Sets the function that will be called by each Component using this action when its primary event is triggered.
-     * @param {Function} fn The function that will be invoked by the action's components.  The function
-     * will be called with no arguments.
-     * @param {Object} scope The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to the Component firing the event.
+<span id='Ext-form-action-Action-method-getParams'>    /**
+</span>     * @private
+     * Get the set of parameters specified in the BasicForm's baseParams and/or the params option.
+     * Items in params override items of the same name in baseParams.
+     * @return {Object} the full set of parameters
      */
-    setHandler : function(fn, scope){
-        this.initialConfig.handler = fn;
-        this.initialConfig.scope = scope;
-        this.callEach('setHandler', [fn, scope]);
+    getParams: function() {
+        return Ext.apply({}, this.params, this.form.baseParams);
     },
 
-<span id='Ext-Action-method-each'>    /**
-</span>     * Executes the specified function once for each Component currently tied to this Action.  The function passed
-     * in should accept a single argument that will be an object that supports the basic Action config/method interface.
-     * @param {Function} fn The function to execute for each component
-     * @param {Object} scope The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed.  Defaults to the Component.
+<span id='Ext-form-action-Action-method-createCallback'>    /**
+</span>     * @private
+     * Creates a callback object.
      */
-    each : function(fn, scope){
-        Ext.each(this.items, fn, scope);
+    createCallback: function() {
+        var me = this,
+            undef,
+            form = me.form;
+        return {
+            success: me.onSuccess,
+            failure: me.onFailure,
+            scope: me,
+            timeout: (this.timeout * 1000) || (form.timeout * 1000),
+            upload: form.fileUpload ? me.onSuccess : undef
+        };
     },
 
-    // private
-    callEach : function(fnName, args){
-        var items = this.items,
-            i = 0,
-            len = items.length;
-            
-        for(; i &lt; len; i++){
-            items[i][fnName].apply(items[i], args);
-        }
-    },
+    statics: {
+<span id='Ext-form-action-Action-property-CLIENT_INVALID'>        /**
+</span>         * @property CLIENT_INVALID
+         * Failure type returned when client side validation of the Form fails
+         * thus aborting a submit action. Client side validation is performed unless
+         * {@link Ext.form.action.Submit#clientValidation} is explicitly set to &lt;tt&gt;false&lt;/tt&gt;.
+         * @type {String}
+         * @static
+         */
+        CLIENT_INVALID: 'client',
 
-    // private
-    addComponent : function(comp){
-        this.items.push(comp);
-        comp.on('destroy', this.removeComponent, this);
-    },
+<span id='Ext-form-action-Action-property-SERVER_INVALID'>        /**
+</span>         * @property SERVER_INVALID
+         * &lt;p&gt;Failure type returned when server side processing fails and the {@link #result}'s
+         * &lt;tt&gt;success&lt;/tt&gt; property is set to &lt;tt&gt;false&lt;/tt&gt;.&lt;/p&gt;
+         * &lt;p&gt;In the case of a form submission, field-specific error messages may be returned in the
+         * {@link #result}'s &lt;tt&gt;errors&lt;/tt&gt; property.&lt;/p&gt;
+         * @type {String}
+         * @static
+         */
+        SERVER_INVALID: 'server',
+
+<span id='Ext-form-action-Action-property-CONNECT_FAILURE'>        /**
+</span>         * @property CONNECT_FAILURE
+         * Failure type returned when a communication error happens when attempting
+         * to send a request to the remote server. The {@link #response} may be examined to
+         * provide further information.
+         * @type {String}
+         * @static
+         */
+        CONNECT_FAILURE: 'connect',
+
+<span id='Ext-form-action-Action-property-LOAD_FAILURE'>        /**
+</span>         * @property LOAD_FAILURE
+         * Failure type returned when the response's &lt;tt&gt;success&lt;/tt&gt;
+         * property is set to &lt;tt&gt;false&lt;/tt&gt;, or no field values are returned in the response's
+         * &lt;tt&gt;data&lt;/tt&gt; property.
+         * @type {String}
+         * @static
+         */
+        LOAD_FAILURE: 'load'
 
-    // private
-    removeComponent : function(comp){
-        Ext.Array.remove(this.items, comp);
-    },
 
-<span id='Ext-Action-method-execute'>    /**
-</span>     * Executes this Action manually using the handler function specified in the original config object
-     * or the handler function set with &lt;code&gt;{@link #setHandler}&lt;/code&gt;.  Any arguments passed to this
-     * function will be passed on to the handler function.
-     * @param {Mixed} arg1 (optional) Variable number of arguments passed to the handler function
-     * @param {Mixed} arg2 (optional)
-     * @param {Mixed} etc... (optional)
-     */
-    execute : function(){
-        this.initialConfig.handler.apply(this.initialConfig.scope || Ext.global, arguments);
     }
 });
 </pre>