Home page and news page are finished
This commit is contained in:
60
resources/assets/js/functions/loadMoreNews.js
vendored
Normal file
60
resources/assets/js/functions/loadMoreNews.js
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
(function ($) {
|
||||
/**
|
||||
* @param {object} _options
|
||||
*/
|
||||
$.fn.loadMoreNews = function (_options) {
|
||||
var nextPage = 2;
|
||||
var isLoading = false;
|
||||
var page = 1;
|
||||
var options = {
|
||||
loadingElementId: '#loading',
|
||||
container: '',
|
||||
url: document.location.pathname
|
||||
};
|
||||
|
||||
$.extend(options, _options);
|
||||
var $isLoading = $(options.loadingElementId, this);
|
||||
$isLoading.hide();
|
||||
|
||||
this.click(function (e) {
|
||||
e.preventDefault();
|
||||
if (!isLoading) {
|
||||
// Set flag and update UI
|
||||
isLoading = 1;
|
||||
$isLoading.show();
|
||||
var $button = $(this).attr("disabled", "disabled");
|
||||
var $container = $(options.container)
|
||||
|
||||
// Fire request for the next page
|
||||
$.ajax({url: options.url + (options.url.indexOf('?') >= 0 ? '&' : '?') + '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;
|
||||
}
|
||||
|
||||
$container.each(function () {
|
||||
var id = this.toString();
|
||||
$(id).append($('<div>'+data+'</div>').find(id).length ? $('<div>'+data+'</div>').find(id).children() : $(data));
|
||||
});
|
||||
++nextPage;
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
}(jQuery));
|
||||
|
||||
$(function () {
|
||||
$('[data-loadmorenews]').each(function () {
|
||||
$(this).loadMoreNews($(this).data('loadmorenews'));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user