Bug Tracker

Changeset 462

Show
Ignore:
Timestamp:
10/23/06 23:29:51 (2 years ago)
Author:
malsup
Message:

Update to support preferred style for plugin options.
Also added support for dataType option.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/form/form.js

    r449 r462  
    22 * ajaxSubmit() provides a mechanism for submitting an HTML form using AJAX. 
    33 * 
    4  * All arguments are optional. 
    5  * 
    6  * If a target arg is provided, it is used to identify the element(s) to be updated with the  
    7  * server response.  The target argument can be a jQuery selector string, a jQuery object, or a DOM element. 
    8  * 
    9  * If a post-submit callback method is provided it is invoked after the response has been returned 
    10  * from the server.   
    11  * 
    12  * If neither target or post-submit arguments are provided then the data returned from the server (if any) 
    13  * is evaluated in the global context. 
    14  * 
    15  * The pre-submit callback can be provided as a hook for running pre-submit logic or for validating the 
    16  * form data.  If the pre-submit callback returns false the form will not be submitted. The pre-submit callback 
    17  * is invoked with two arguments, the form data in array format, and the jQuery object.  The form 
    18  * data array takes the form: 
     4 * Options are provided via an options object.  The following options are supported: 
     5 * 
     6 *  target:   Identifies the element(s) in the page to be updated with the server response.   
     7 *            This value may be specified as a jQuery selection string, a jQuery object,  
     8 *            or a DOM element. 
     9 *            default value: null 
     10 * 
     11 *  url:      URL to which the form data will be submitted. 
     12 *            default value: value of form's 'action' attribute 
     13 * 
     14 *  method:   The method in which the form data should be submitted, 'GET' or 'POST'. 
     15 *            default value: value of form's 'method' attribute (or 'GET' if none found) 
     16 * 
     17 *  before:   Callback method to be invoked before the form is submitted. 
     18 *            default value: null 
     19 * 
     20 *  after:    Callback method to be invoked after the form has been successfully submitted. 
     21 *            default value: null 
     22 * 
     23 *  dataType: Expected dataType of the response.  One of: null, 'xml', 'script', or 'json' 
     24 *            default value: null 
     25 * 
     26 *  semantic: Boolean flag indicating whether data must be submitted in semantic order (slower). 
     27 *            default value: false 
     28 * 
     29 * 
     30 * The 'before' callback can be provided as a hook for running pre-submit logic or for  
     31 * validating the form data.  If the 'before' callback returns false then the form will  
     32 * not be submitted. The 'before' callback is invoked with two arguments: the form data  
     33 * in array format, and the jQuery object.  The form data array takes the following form: 
    1934 * 
    2035 *     [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] 
    2136 * 
    22  * 
    23  * The url and mth arguments can also be provided to override the form defaults.  If not provided, 
    24  * the form emement's 'action' and 'method' properties will be used. 
    25  * 
    26  * The semantic argument can be used to force form serialization in semantic order.  If your form must 
    27  * be submitted with name/value pairs in semantic order then pass true for this arg, otherwise pass false 
    28  * (or nothing) to avoid the overhead for this logic (which can be significant for very large forms). 
    29  * 
    30  * @example $("#form-id").ajaxSubmit("#destination"); 
    31  * @desc The form is submitted and the resulting HTML contents are loaded into the #destination element. 
    32  * 
    33  * @example $("#form-id").ajaxSubmit(function(){ 
    34  *   alert("all done!"); 
     37 * If an 'after' callback method is provided it is invoked after the response has been returned 
     38 * from the server.  It is passed the responseText or responseXML value (depending on dataType). 
     39 * See jQuery.ajax for further details. 
     40 * 
     41 * The dataType option provides a means for specifying how the server response should be handled. 
     42 * This maps directly to the jQuery.httpData method.  The following values are supported as of 
     43 * jQuery verions 1.0.2: 
     44 *      'xml':    if dataType == 'xml' the server response is treated as XML and the 'after' 
     45 *                   callback method, if specified, will be passed the responseXML value 
     46 *      'json':   if dataType == 'json' the server response will be evaluted and passed to  
     47 *                   the 'after' callback, if specified 
     48 *      'script': if dataType == 'script' the server response is evaluated in the global context 
     49 * 
     50 * Note that it does not make sense to use both the 'target' and 'dataType' options.  If both 
     51 * are provided the target will be ignored. 
     52 * 
     53 * The semantic argument can be used to force form serialization in semantic order.  If your  
     54 * form must be submitted with name/value pairs in semantic order then pass true for this arg,  
     55 * otherwise pass false (or nothing) to avoid the overhead for this logic (which can be  
     56 * significant for very large forms). 
     57 * 
     58 * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this: 
     59 * 
     60 * $("#form-id").submit(function() { 
     61 *     $(this).ajaxSubmit(options); 
     62 *     return false; // cancel conventional submit 
    3563 * }); 
    36  * @desc The form is submitted and a callback is fired, letting you know when it's done. 
    37  * 
    38  * @example $("#form-id").ajaxSubmit(); 
    39  * @desc The form is submitted and the results returned from the server are 
    40  * automatically executed (useful for having the server return more Javascript commands to execute). 
    41  * 
    42  * @example $("#form-id").submit(function() { 
    43  *     $(this).ajaxSubmit("#destination"); 
    44  *     return false; 
     64 * 
     65 * When using ajaxForm(), however, this is done for you. 
     66 * 
     67 * 
     68 * @example  
     69 * var options = { 
     70 *     target: '#myTargetDiv' 
     71 * };    
     72 * $('#myForm').ajaxSubmit(options); 
     73 * @desc Submit form and update page element with server response 
     74 * 
     75 * 
     76 * @example  
     77 * var options = { 
     78 *     after: function(responseText) { 
     79 *         alert(responseText); 
     80 *     } 
     81 * };    
     82 * $('#myForm').ajaxSubmit(options); 
     83 * @desc Submit form and alert the server response 
     84 * 
     85 * 
     86 * @example  
     87 * var options = { 
     88 *     before: function(formArray, jqForm) { 
     89 *         if (formArray.length == 0) { 
     90 *             alert('Please enter data.'); 
     91 *             return false; 
     92 *         } 
     93 *     } 
     94 * };    
     95 * $('#myForm').ajaxSubmit(options); 
     96 * @desc Pre-submit validation which aborts the submit operation if form data is empty 
     97 * 
     98 * 
     99 * @example  
     100 * var options = { 
     101 *     url: myJsonUrl.php, 
     102 *     dataType: 'json', 
     103 *     after: function(data) { 
     104 *        // 'data' is an object representing the the evaluated json data 
     105 *     } 
     106 * };    
     107 * $('#myForm').ajaxSubmit(options); 
     108 * @desc json data returned and evaluated 
     109 * 
     110 * 
     111 * @example  
     112 * var options = { 
     113 *     url: myXmlUrl.php, 
     114 *     dataType: 'xml', 
     115 *     after: function(responseXML) { 
     116 *        // responseXML is XML document object 
     117 *        var data = $('myElement', responseXML).text(); 
     118 *     } 
     119 * };    
     120 * $('#myForm').ajaxSubmit(options); 
     121 * @desc XML data returned from server 
     122 * 
     123 * 
     124 * @example 
     125 * $('#myForm).submit(function() { 
     126 *    $(this).ajaxSubmit(); 
     127 *    return false; 
    45128 * }); 
    46  * @desc Typical way of binding this method to a form's submit event. 
     129 * @desc Bind form's submit event to use ajaxSubmit 
     130 * 
    47131 * 
    48132 * @name ajaxSubmit 
    49133 * @type jQuery 
    50  * @param target   jQuery selector string, jQuery object or DOM element which identifies the element(s) to update with the server response 
    51  * @param post_cb  callback to be invoked after any results are returned 
    52  * @param pre_cb   callback to be invoked after gathering the form data but before submitting the form to the server 
    53  * @param url      url to invoke (if provided this will override the form's 'action' attribute) 
    54  * @param mth      method to use, 'POST' or 'GET' (if provided this will override the form's 'method' attribute) 
    55  * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) 
     134 * @param options  object literal containing options which control the form submission process 
    56135 * @return jQuery  
    57136 * @see formToArray 
     
    61140 * @author jQuery Community 
    62141 */ 
    63 jQuery.fn.ajaxSubmit = function(target, post_cb, pre_cb, url, mth, semantic) { 
    64     var a = this.formToArray(semantic); 
     142jQuery.fn.ajaxSubmit = function(options) { 
     143    options = jQuery.extend({ 
     144        target:   null, 
     145        url:      this.attr('action') || '', 
     146        method:   this.attr('method') || 'GET', 
     147        before:   null, 
     148        after:    null, 
     149        dataType: null, // 'xml', 'script', or 'json' (@see jQuery.httpData) 
     150        semantic: false 
     151    }, options || {}); 
    65152     
    66     // give pre-callback opportunity to abort the submit 
    67     if (pre_cb && pre_cb.constructor == Function && pre_cb(a, this) === false) return; 
    68  
    69     url = url || this.attr('action') || ''; 
    70     mth = (mth || this.attr('method') || 'GET').toUpperCase(); 
     153    var a = this.formToArray(options.semantic); 
     154     
     155    // give pre-submit callback an opportunity to abort the submit 
     156    if (options.before && options.before(a, this) === false) return; 
     157 
    71158    var q = jQuery.param(a); 
    72     var get = mth == 'GET'; 
    73  
    74     if (get) url = url + '?' + q; 
    75      
    76     // if no target or 'post' callback was provided then default to a callback 
    77     // that evals the response 
    78     var t = target || post_cb || function(r) { 
    79             if (r.responseText) eval.call(window, r.responseText) 
    80         }; 
    81  
    82     if (t && t.constructor != Function) 
    83         jQuery(t).load(url, get ? null : a, post_cb); 
     159    var get = (options.method && options.method.toUpperCase() == 'GET'); 
     160 
     161    if (get) 
     162        // if url already has a '?' then append args after '&' 
     163        options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; 
     164 
     165    // perform a load on the target only if dataType is not provided 
     166    if (!options.dataType && options.target)  
     167        jQuery(options.target).load(options.url, get ? null : a, options.after); 
    84168    else 
    85         jQuery.ajax({ url: url, success: t, data: get ? null : q, type: mth }); 
     169        jQuery.ajax({  
     170            url:      options.url,  
     171            success:  options.after,  
     172            type:     options.method,  
     173            dataType: options.dataType, 
     174            data:     get ? null : q // data is null for 'get' or the query string for 'post' 
     175        }); 
    86176    return this; 
    87177}; 
     
    93183 * The advantages of using this method instead of ajaxSubmit() are: 
    94184 * 
    95  * 1: This method will include coordinates for <input type="image" /> elements (if the element is used to submit the form). 
    96  * 2. This method will include the submit element's name/value data (for the element that was used to submit the form). 
     185 * 1: This method will include coordinates for <input type="image" /> elements (if the element  
     186 *    is used to submit the form).  
     187 * 2. This method will include the submit element's name/value data (for the element that was  
     188 *    used to submit the form). 
    97189 * 3. This method binds the submit() method to the form for you. 
    98190 * 
    99  * Note that for accurate x/y coordinates of image submit elements in ALL browsers 
     191 * Note that for accurate x/y coordinates of image submit elements in all browsers 
    100192 * you need to also use the "dimensions" plugin (this method will auto-detect its presence). 
    101193 * 
    102  * All arguments are optional. 
    103  * 
    104  * If a target arg is provided, it is used to identify the element(s) to be updated with the  
    105  * server response.  The target argument can be a jQuery selector string, a jQuery object, or a DOM element. 
    106  * 
    107  * If a post-submit callback method is provided it is invoked after the response has been returned 
    108  * from the server.   
    109  * 
    110  * If neither target or post-submit arguments are provided then the data returned from the server (if any) 
    111  * is evaluated in the global context. 
    112  * 
    113  * The pre-submit callback can be provided as a hook for running pre-submit logic or for validating the 
    114  * form data.  If the pre-submit callback returns false the form will not be submitted. The pre-submit callback 
    115  * is invoked with two arguments, 1) the form data in array format, and 2) the jQuery object.  The form 
    116  * data array takes the form: 
    117  * 
    118  *     [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] 
    119  * 
    120  * 
    121  * The semantic argument can be used to force form serialization in semantic order.  If your form must 
    122  * be submitted with name/value pairs in semantic order then pass true for this arg, otherwise pass false 
    123  * (or nothing) to avoid the overhead for this logic (which can be significant for very large forms). 
    124  * 
    125  * @example $('#form-id').ajaxForm(); 
    126  * @desc Just eval the results returned from the backend. 
    127  * 
    128  * @example $('#form-id').ajaxForm('#target-id'); 
    129  * @desc Render backend results directly to target ID (expects (x)HTML). 
    130  * 
    131  * @example $('#form-id').ajaxForm(post_callback); 
    132  * @desc Submit to backend URL (form action) then call this function. 
    133  * 
    134  * @example $('#form-id').ajaxForm('#target-id', post_callback); 
    135  * @desc Load target ID with backend results then call a function. 
    136  * 
    137  * @example $('#form-id').ajaxForm('#target-id', null, pre_callback); 
    138  * @desc Call a browser function (for validation) and then load server results to target ID. 
    139  * 
    140  * @example $('#form-id').ajaxForm('#target-id', post_callback, pre_callback); 
    141  * @desc Call validation function first then load server results to target ID and then call post_callback function. 
    142  * 
    143  * @name ajaxForm 
    144  * @param target   jQuery selector string, jQuery object or DOM element which identifies the element(s) to update with the server response 
    145  * @param post_cb  callback to be invoked after any results are returned 
    146  * @param pre_cb   callback to be invoked after gathering the form data but before submitting the form to the server 
    147  * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) 
     194 * The options argument for ajaxForm works exactly as it does for ajaxSubmit.  ajaxForm merely 
     195 * passes the options argument along after properly binding events for submit elements and 
     196 * the form itself.  See ajaxSubmit for a full description of the options argument. 
     197 * 
     198 * 
     199 * @example  
     200 * var options = { 
     201 *     target: '#myTargetDiv' 
     202 * };    
     203 * $('#myForm').ajaxSForm(options); 
     204 * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response 
     205 *       when the form is submitted. 
     206 * 
     207 * 
     208 * @example  
     209 * var options = { 
     210 *     after: function(responseText) { 
     211 *         alert(responseText); 
     212 *     } 
     213 * };    
     214 * $('#myForm').ajaxSubmit(options); 
     215 * @desc Bind form's submit event so that server response is alerted after the form is submitted. 
     216 * 
     217 * 
     218 * @example  
     219 * var options = { 
     220 *     before: function(formArray, jqForm) { 
     221 *         if (formArray.length == 0) { 
     222 *             alert('Please enter data.'); 
     223 *             return false; 
     224 *         } 
     225 *     } 
     226 * };    
     227 * $('#myForm').ajaxSubmit(options); 
     228 * @desc Bind form's submit event so that pre-submit callback is invoked before the form 
     229 *       is submitted. 
     230 * 
     231 * 
     232 * @name   ajaxForm 
     233 * @param  options  object literal containing options which control the form submission process 
    148234 * @return jQuery 
    149  * @type jQuery 
    150  * @see formToArray 
    151  * @see ajaxSubmit 
     235 * @type   jQuery 
     236 * @see    ajaxSubmit 
    152237 * @author jQuery Community 
    153238 */ 
    154 jQuery.fn.ajaxForm = function(target, post_cb, pre_cb, semantic) { 
     239jQuery.fn.ajaxForm = function(options) { 
    155240    return this.each(function() { 
    156241        jQuery("input[@type=submit],input[@type=image]", this).click(function(ev) { 
     
    160245                this.form.clk_x = ev.offsetX; 
    161246                this.form.clk_y = ev.offsetY; 
    162             } else if (typeof jQuery.fn.offset == 'function') { // use dimensions plugin if it's installed 
     247            } else if (typeof jQuery.fn.offset == 'function') { // try to use dimensions plugin 
    163248                var offset = $(this).offset(); 
    164249                this.form.clk_x = ev.pageX - offset.left; 
     
    170255        }) 
    171256    }).submit(function(e) { 
    172         jQuery(this).ajaxSubmit(target, post_cb, pre_cb, null, null, semantic); 
     257        jQuery(this).ajaxSubmit(options); 
    173258        return false; 
    174259    }); 
     
    187272 * ajaxSubmit() and ajaxForm() methods. 
    188273 * 
    189  * The semantic argument can be used to force form serialization in semantic order.  If your form must 
    190  * be submitted with name/value pairs in semantic order then pass true for this arg, otherwise pass false 
    191  * (or nothing) to avoid the overhead for this logic (which can be significant for very large forms). 
     274 * The semantic argument can be used to force form serialization in semantic order.   
     275 * If your form must be submitted with name/value pairs in semantic order then pass  
     276 * true for this arg, otherwise pass false (or nothing) to avoid the overhead for  
     277 * this logic (which can be significant for very large forms). 
    192278 * 
    193279 * @example var data = $("#myForm").formToArray(); 
    194280 * $.post( "myscript.cgi", data ); 
    195  * @desc Collect all the data from a form and submit it to a server-side application. 
     281 * @desc Collect all the data from a form and submit it to the server. 
    196282 * 
    197283 * @name formToArray 
     
    237323 * in the format: name1=value1&name2=value2 
    238324 * 
    239  * The semantic argument can be used to force form serialization in semantic order.  If your form must 
    240  * be submitted with name/value pairs in semantic order then pass true for this arg, otherwise pass false 
    241  * (or nothing) to avoid the overhead for this logic (which can be significant for very large forms). 
     325 * The semantic argument can be used to force form serialization in semantic order.   
     326 * If your form must be submitted with name/value pairs in semantic order then pass  
     327 * true for this arg, otherwise pass false (or nothing) to avoid the overhead for  
     328 * this logic (which can be significant for very large forms). 
    242329 * 
    243330 * @example var data = $("#myForm").serialize();