Last active
August 29, 2015 14:18
-
-
Save jprieton/7b15313e07dfae5e95f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
$.fn.ajaxFormSubmit = function (options) { | |
var settings = $.extend({ | |
alertContainer: '', | |
callback: null | |
}, options); | |
if (typeof admin_url === 'undefined') | |
return this; | |
var fields = $(this).find('input, select, textarea').not(':disabled'); | |
$(this).ajaxForm({ | |
dataType: 'json', | |
beforeSubmit: function (selectorData, selector, options) { | |
$(selector).find('*[type=submit]') | |
.removeClass('btn-danger shake') | |
.attr('disabled', '') | |
.html('Enviando <i class="fa fa-spinner fa-spin"></i>'); | |
$(fields).attr({disabled: 'disabled'}); | |
}, | |
success: function (response, statusText, xhr, selector) { | |
$(selector).find('*[type=submit]') | |
.removeClass('btn-danger shake') | |
.removeAttr('disabled'); | |
$(fields).removeAttr('disabled'); | |
$(settings.alertContainer).empty(); | |
if (response.success === true) { | |
$(selector).find('*[type=submit]') | |
.addClass(' btn-success') | |
.html('Éxito <i class="fa fa-check"></i>'); | |
$(settings.alertContainer).bootstrapAlert({ | |
type: 'success', | |
message: '<strong>Éxito:</strong> ' + response.data[0].message | |
}); | |
$(selector)[0].reset(); | |
} else { | |
$(selector).find('*[type=submit]') | |
.addClass('btn-danger shake') | |
.html('Datos incorrectos <i class="fa fa-warning"></i>'); | |
$(settings.alertContainer).bootstrapAlert({ | |
type: 'danger', | |
message: '<strong>Error:</strong> ' + response.data[0].message | |
}); | |
} | |
if (typeof settings.callback === 'function') { | |
settings.callback(this, response); | |
} | |
} | |
}); | |
return this; | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#test-form').ajaxFormSubmit({ | |
alertContainer: '#alert-container', | |
callback: function (this, response) { | |
return null; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment