Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorter plugin initialization and set reset/clear form options through data attributes #400

Merged
merged 2 commits into from
Feb 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions jquery.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,37 @@ $.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)
if (!this.length) {
log('ajaxSubmit: skipping submit process - no element selected');
return this;
}

var method, action, url, $form = this;

if (typeof options == 'function') {
options = { success: options };
}
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 All @@ -117,11 +133,13 @@ $.fn.ajaxSubmit = function(options) {
// clean url (don't include hash vaue)
url = (url.match(/^([^#]+)/)||[])[1];
}

options = $.extend(true, {
url: url,
success: $.ajaxSettings.success,
type: method || $.ajaxSettings.type,
resetForm : ($form.attr2('data-reset') === 'true'),
clearForm : ($form.attr2('data-clear') === 'true'),
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank'
}, options);

Expand Down Expand Up @@ -848,10 +866,24 @@ $.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 || {};
options.delegation = options.delegation && $.isFunction($.fn.on);
$.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
if (!options.delegation && this.length === 0) {
var o = { s: this.selector, c: this.context };
Expand Down Expand Up @@ -885,6 +917,7 @@ $.fn.ajaxForm = function(options) {
function doAjaxSubmit(e) {
/*jshint validthis:true */
var options = e.data;

if (!e.isDefaultPrevented()) { // if event has been canceled, don't proceed
e.preventDefault();
$(e.target).ajaxSubmit(options); // #365
Expand Down