Skip to content

Instantly share code, notes, and snippets.

@frugan-dev
Created July 26, 2017 10:10
Show Gist options
  • Save frugan-dev/a166a580239b33cec94eead23e9f72b6 to your computer and use it in GitHub Desktop.
Save frugan-dev/a166a580239b33cec94eead23e9f72b6 to your computer and use it in GitHub Desktop.
form[data-async]
//https://gist.github.com/havvg/3226804
//http://1000hz.github.io/bootstrap-validator/
//$(document).validator().on('submit', 'form[data-async]', function(e) { // va in conflitto con form data-sync
$('form[data-async]').validator().on('submit', function(e) {
$.fix_theme_callback();
if (!e.isDefaultPrevented()) {
e.preventDefault();
var $form = $(this);
var $target = $form.attr('data-target');
var $response = $form.find('.response');
var $btn = $form.find('button[type="submit"]').button('loading');
var $dismiss = '<button type="button" class="close" data-dismiss="alert" aria-label="' + js_obj.text_close + '"><span aria-hidden="true">&times;</span></button>';
//http://stackoverflow.com/a/15824920
if ( $.trim( $response.html() ).length ) {
$response.html( '<div class="alert alert-info alert-dismissible fade in" role="alert">' + $dismiss + $btn.attr('data-loading-text') + '</div>' );
}
$.ajax({
type: $form.attr('method'), //You should use type if you're using versions of jQuery prior to 1.9.0.
dataType: 'json',
url: js_obj.admin_ajax,
data: $form.serialize(),
}).always(function (data) {
var $status = ( typeof data.status !== 'undefined' ) ? data.status : 'danger';
var $message = ( typeof data.message !== 'undefined' ) ? data.message : js_obj.text_error;
var $message_html = '<div class="alert alert-' + $status + ' alert-dismissible fade in" role="alert">' + $dismiss + $message + '</div>';
var $data_html = ( typeof data.html !== 'undefined' ) ? data.html : '<div class="alert alert-' + $status + ' fade in" role="alert">' + $message + '</div>';
if($target) {
$($target).html( $data_html );
} else {
$response.html( $message_html );
}
if( typeof data.redirect !== 'undefined' ) {
//setTimeout( function() {
parent.location.href = data.redirect;
//}, 1000 );
} else if( typeof data.reload !== 'undefined' ) {
window.location.reload( true );
}
$btn.button('reset');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment