Skip to content

Commit b91ed88

Browse files
committed
add always callback for ajax requests
1 parent b78fd8d commit b91ed88

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ protected function _ajax($method, $url, $responseElement = '', $parameters = [])
7070
}
7171
$this->createAjaxParameters($ajaxParameters, $parameters);
7272
$retour .= "$.ajax({" . $this->implodeAjaxParameters($ajaxParameters) . "}).done(function( data, textStatus, jqXHR ) {\n";
73-
$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader, ($historize ? $originalSelector : null)) . "})";
73+
$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, ($historize ? $originalSelector : null)) . "})";
7474
if (isset($error)) {
7575
$retour .= '.fail(function( jqXHR, textStatus, errorThrown ){' . $error . '})';
7676
}
77+
$retour .= '.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) {' . ($always ?? '') . $this->removeLoader($hasLoader) . '})';
7778
$retour .= ";\n";
7879
$retour = $this->_addJsCondition($jsCondition, $retour);
7980
if ($immediatly) {
@@ -152,7 +153,7 @@ protected function autoActiveLinks($previousURL = "window.location.href") {
152153
return $result;
153154
}
154155

155-
protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader = false, $history = null) {
156+
protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $history = null) {
156157
$retour = "";
157158
$call = null;
158159
if (JString::isNotNull($responseElement)) {
@@ -172,17 +173,20 @@ protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition
172173
}
173174
$retour .= "\nwindow.history.pushState({'html':data,'selector':" . Javascript::prep_value($history) . ",'jqueryDone':'{$jqueryDone}'},'', url);";
174175
}
175-
if ($hasLoader === 'internal') {
176-
$retour .= "\n$(self).removeClass('loading');";
177-
} elseif ($hasLoader === 'internal-x') {
178-
$retour .= "\n$(self).children('.ajax-loader').remove();";
179-
} else {
180-
$retour .= "\n$(self).find('.loading').removeClass('loading');";
181-
}
182176
$retour .= "\t" . $jsCallback . "\n";
183177
return $retour;
184178
}
185179

180+
protected function removeLoader($hasLoader) {
181+
if ($hasLoader === 'internal') {
182+
return "\n$(self).removeClass('loading');";
183+
}
184+
if ($hasLoader === 'internal-x') {
185+
return "\n$(self).children('.ajax-loader').remove();";
186+
}
187+
return "\n$(self).find('.loading').removeClass('loading');";
188+
}
189+
186190
protected function _getResponseElement($responseElement) {
187191
if (JString::isNotNull($responseElement)) {
188192
$responseElement = Javascript::prep_jquery_selector($responseElement);
@@ -821,10 +825,11 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
821825
}
822826
$this->createAjaxParameters($ajaxParameters, $parameters);
823827
$retour .= "$.ajax({" . $this->implodeAjaxParameters($ajaxParameters) . "}).done(function( data ) {\n";
824-
$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader) . "})";
828+
$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback) . "})";
825829
if (isset($error)) {
826830
$retour .= '.fail(function( jqXHR, textStatus, errorThrown ){' . $error . '})';
827831
}
832+
$retour .= '.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) {' . ($always ?? '') . $this->removeLoader($hasLoader) . '})';
828833
$retour .= ";\n";
829834
if ($validation) {
830835
$retour = "$('#'+" . $form . ").validate({submitHandler: function(form) {

0 commit comments

Comments
 (0)