Skip to content

Commit

Permalink
Shorter plugin initialization and set reset/clear form options throug…
Browse files Browse the repository at this point in the history
…h data attributes

Copied from #400
Thanks to @izziaraffaele
  • Loading branch information
kevindb committed Jan 15, 2017
1 parent 27fd965 commit 748a67c
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions jquery.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ feature.formdata = window.FormData !== undefined;
var hasProp = !!$.fn.prop;

// attr2 uses prop when it can but checks the return type for
// an expected string. this accounts for the case where a form
// an expected string. this accounts for the case where a form
// contains inputs with names like "action" or "method"; in those
// cases "prop" returns the element
$.fn.attr2 = function() {
Expand All @@ -89,8 +89,13 @@ $.fn.attr2 = function() {
/**
* ajaxSubmit() provides a mechanism for immediately submitting
* an HTML form using AJAX.
*
* @param object|string options jquery.form.js parameters or custom url for submission
* @param object data extraData
* @param string dataType ajax dataType
* @param function onSuccess ajax success callback function
*/
$.fn.ajaxSubmit = function(options) {
$.fn.ajaxSubmit = function(options, data, dataType, onSuccess) {
/*jshint scripturl:true */

// fast fail if nothing selected (http://dev.jquery.com/ticket/2752)
Expand All @@ -103,8 +108,20 @@ $.fn.ajaxSubmit = function(options) {

if (typeof options == 'function') {
options = { success: options };
}
else if ( options === undefined ) {

} else if ( typeof options == 'string' || ( options === false && arguments.length > 0 ) ) {
options = {
'url' : options,
'data' : data,
'dataType' : dataType,
};

if(typeof onSuccess == 'function')
{
options.success = onSuccess;
}

} else if ( options === undefined ) {
options = {};
}

Expand Down Expand Up @@ -460,17 +477,17 @@ $.fn.ajaxSubmit = function(options) {

var CLIENT_TIMEOUT_ABORT = 1;
var SERVER_ABORT = 2;

function getDoc(frame) {
/* it looks like contentWindow or contentDocument do not
* carry the protocol property in ie8, when running under ssl
* frame.document is the only valid response document, since
* the protocol is know but not on the other two objects. strange?
* "Same origin policy" http://en.wikipedia.org/wiki/Same_origin_policy
*/

var doc = null;

// IE8 cascading access check
try {
if (frame.contentWindow) {
Expand Down Expand Up @@ -506,8 +523,8 @@ $.fn.ajaxSubmit = function(options) {
// take a breath so that pending repaints get some cpu time before the upload starts
function doSubmit() {
// make sure form attrs are set
var t = $form.attr2('target'),
a = $form.attr2('action'),
var t = $form.attr2('target'),
a = $form.attr2('action'),
mp = 'multipart/form-data',
et = $form.attr('enctype') || $form.attr('encoding') || mp;

Expand Down Expand Up @@ -618,7 +635,7 @@ $.fn.ajaxSubmit = function(options) {
if (xhr.aborted || callbackProcessed) {
return;
}

doc = getDoc(io);
if(!doc) {
log('cannot access response document');
Expand Down Expand Up @@ -847,8 +864,20 @@ $.fn.ajaxSubmit = function(options) {
* passes the options argument along after properly binding events for submit elements and
* the form itself.
*/
$.fn.ajaxForm = function(options) {
options = options || {};
$.fn.ajaxForm = function(options, data, dataType, onSuccess) {
if (typeof options == 'string' || (options === false && arguments.length > 0)) {
options = {
'url' : options,
'data' : data,
'dataType' : dataType,
};

if (typeof onSuccess == 'function') {
options.success = onSuccess;
}
}

options = options || {};
options.delegation = options.delegation && $.isFunction($.fn.on);

// in jQuery 1.3+ we can fix mistakes with the ready state
Expand Down Expand Up @@ -964,7 +993,7 @@ $.fn.formToArray = function(semantic, elements, filtering) {
if (!els || !els.length) {
return a;
}

if ($.isFunction(filtering)) {
var els = $.map(els, filtering);
}
Expand Down

0 comments on commit 748a67c

Please sign in to comment.