abpAjaxForm enhancements

pull/279/head
Halil İbrahim Kalkan 7 years ago
parent a8174f220e
commit bc96419540

@ -1,5 +1,21 @@
(function($) {
(function ($) {
function enableAjaxForm($forms, validate) {
if ($forms.length) {
$forms.each(function () {
var $form = $(this);
if (validate === true) {
$.validator.unobtrusive.parse($form);
}
if ($form.attr('data-ajaxForm') === 'true') {
$form.abpAjaxForm();
}
});
}
}
abp.dom.onNodeAdded(function (args) {
args.$el.findWithSelf('[data-toggle="tooltip"]').tooltip({
container: 'body'
@ -9,12 +25,7 @@
container: 'body'
});
var $forms = args.$el.findWithSelf('form');
if ($forms.length) {
$forms.each(function() {
$.validator.unobtrusive.parse($(this));
});
}
enableAjaxForm(args.$el.findWithSelf('form'), true);
});
abp.dom.onNodeRemoved(function (args) {
@ -31,6 +42,8 @@
$('[data-toggle="popover"]').popover({
container: 'body'
});
enableAjaxForm($('form'));
});
})(jQuery);

@ -66,18 +66,19 @@ $.validator.defaults.ignore = ''; //TODO: Would be better if we can apply only f
_$modal = _$modalContainer.find('.modal');
_$form = _$modalContainer.find('form');
if (_$form.length) {
if (_$form.attr('data-ajaxForm') !== 'false') {
_$form.abpAjaxForm({
success: function() {
_publicApi.setResult.apply(_publicApi, arguments);
_$modal.modal('hide');
}
});
if (_$form.attr('data-ajaxForm') === undefined || _$form.attr('data-ajaxForm') === false) {
_$form.abpAjaxForm();
}
_$form.on('abp-ajax-success',
function() {
_publicApi.setResult.apply(_publicApi, arguments);
_$modal.modal('hide');
});
} else {
_$form = null;
}
_$modal.modal({
backdrop: 'static'
});

@ -4,12 +4,13 @@
}
$.fn.abpAjaxForm = function (userOptions) {
var $form = $(this);
userOptions = userOptions || {};
var options = $.extend({}, $.fn.abpAjaxForm.defaults, userOptions);
options.beforeSubmit = function (arr, $form) {
if (userOptions.beforeSubmit && userOptions.beforeSubmit.apply(this, arguments) === false) {
if ((userOptions.beforeSubmit && userOptions.beforeSubmit.apply(this, arguments)) === false) {
return false;
}
@ -19,10 +20,26 @@
$form.find("button[type='submit']").buttonBusy(true);
//TODO: Disable other buttons..?
return true;
};
options.success = function (responseText, statusText, xhr, $form) {
userOptions.success && userOptions.success.apply(this, arguments);
$form.trigger('abp-ajax-success',
{
responseText: responseText,
statusText: statusText,
xhr: xhr,
$form: $form
});
};
options.error = function (jqXhr) {
if ((userOptions.error && userOptions.error.apply(this, arguments)) === false) {
return;
}
if (jqXhr.getResponseHeader('_AbpErrorFormat') === 'true') {
abp.ajax.logError(jqXhr.responseJSON.error);
var messagePromise = abp.ajax.showError(jqXhr.responseJSON.error);
@ -34,18 +51,23 @@
}
};
//TODO: Error?
options.complete = function (jqXhr, status, $form) {
if ($.contains(document, $form[0])) {
$form.find("button[type='submit']").buttonBusy(false);
//TODO: Re-enable other buttons..?
}
$form.trigger('abp-ajax-complete',
{
status: status,
jqXhr: jqXhr,
$form: $form
});
userOptions.complete && userOptions.complete.apply(this, arguments);
};
return this.ajaxForm(options);
return $form.ajaxForm(options);
};
$.fn.abpAjaxForm.defaults = {

Loading…
Cancel
Save