Files
nhgooi.nl/public/js/main.js
2024-03-13 12:37:09 +01:00

42 lines
1.3 KiB
JavaScript
Vendored

function loadMoreNews(e, _options) {
var options = $.extend( {
loadingElementId: '#loading'
}, _options );
window.loadMoreNews = window.loadMoreNewsSettings ?? {}
var nextPage = window.loadMoreNewsSettings.nextPage ?? 2;
var isLoading = window.loadMoreNewsSettings.isLoading ?? false;
var $isLoading = $(options.loadingElementId).hide();
e.preventDefault();
if (!isLoading) {
// Set flag and update UI
isLoading = 1;
$isLoading.show();
var $button = $(this).attr("disabled", "disabled");
// Fire request for the next page
$.ajax({url: document.location.pathname + '?pagina=' + nextPage})
.always(function () {
// Whether success or failure, update the UI again
isLoading = 0;
$isLoading.hide();
$button.removeAttr("disabled");
})
.done(function (data) {
if (!data) {
// When no data was returned, disable the button permanently
page = -1;
$button.attr("disabled", "disabled").text("Geen nieuws meer.");
return;
}
console.log($(data));
console.log($(data).find('#items-more-news'));
$('#items-more-news').append($(data).find('#items-more-news'));
$('#items-most-read').append($(data).find('#items-most-read'));
++nextPage;
});
}
}