Skip to content

Instantly share code, notes, and snippets.

@unzeitigm
Last active June 6, 2018 09:17
Show Gist options
  • Save unzeitigm/4159b5c65089415c04ce1e1fbab0ddce to your computer and use it in GitHub Desktop.
Save unzeitigm/4159b5c65089415c04ce1e1fbab0ddce to your computer and use it in GitHub Desktop.
Nette Ajax extensions to disable a button during ajax request
(function ($) {
let Spinner = new window.Spinner();
$.nette.ext('trigger-spinner', {
before: function (jqXHR, settings) {
if (!this.validRequest(settings)) {
return;
}
this.defaultContent = this.trigger.innerHTML;
this.trigger.disabled = true;
this.trigger.innerHTML = '';
this.trigger.appendChild(Spinner.getIconSmall());
},
success: function (payload, status, jqXHR, settings) {
if (!this.validRequest(settings)) {
return;
}
this.trigger.disabled = false;
this.trigger.innerHTML = this.defaultContent;
}
}, {
validRequest: function (settings) {
if (!settings.nette || !settings.nette.el[0]) {
return false;
}
this.trigger = settings.nette.el[0];
return this.trigger.dataset.ajaxSpinner === 'true';
},
trigger: null,
defaultContent: null
});
$.nette.ext('trigger-loading-text', {
before: function (jqXHR, settings) {
if (!this.validRequest(settings)) {
return;
}
this.defaultContent = this.trigger.value;
this.trigger.disabled = true;
this.trigger.value = this.loadingText;
},
success: function (payload, status, jqXHR, settings) {
if (!this.validRequest(settings)) {
return;
}
this.trigger.disabled = false;
this.trigger.value = this.defaultContent;
}
}, {
validRequest: function (settings) {
if (!settings.nette || !settings.nette.el[0]) {
return false;
}
this.trigger = settings.nette.el[0];
if (this.trigger.dataset.ajaxLoadingText !== undefined) {
this.loadingText = this.trigger.dataset.ajaxLoadingText;
return true;
}
return false;
},
trigger: null,
loadingText: null,
defaultContent: null
});
})($);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment