Last active
June 6, 2018 09:17
-
-
Save unzeitigm/4159b5c65089415c04ce1e1fbab0ddce to your computer and use it in GitHub Desktop.
Nette Ajax extensions to disable a button during ajax request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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