Skip to content

Instantly share code, notes, and snippets.

@aviogreen
Created October 24, 2015 12:50
Show Gist options
  • Save aviogreen/df37d695386d4519a288 to your computer and use it in GitHub Desktop.
Save aviogreen/df37d695386d4519a288 to your computer and use it in GitHub Desktop.
$('[data-ajax=modal]').each(function(index, el) {
var this_el = $(this);
var form_el = $('form', this_el);
this_el.on('hide.bs.modal', function(){
var next_el = $('.modal-header', this_el).next();
if( next_el.hasClass('alert') )
next_el.remove();
});
form_el.ajaxForm({
beforeSubmit: function(res){
$('[type=submit]', form_el).prop('disabled', true);
},
success: function(res){
form_el.ajaxSubmit(function(){
var next_el = $('.modal-header', this_el).next();
if( next_el.hasClass('alert') )
next_el.remove();
$('[type=submit]', form_el).prop('disabled', false);
var alert = $('<div/>').addClass('alert').html(res.message);
if( res.success )
alert.addClass('alert-success');
else
alert.addClass('alert-danger');
if( res.updated ){
$.each(res.updated, function(index, value) {
$('[data-live-update=' + index + ']').html(value);
});
}
alert.insertAfter( $('.modal-header', this_el) );
});
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment