CODE:
<script>
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function ($scope, $firebaseArray, $timeout) {
console.log(<%=lastID%>);
$scope.data = [];
var _n = Math.ceil(($(window).height() - 50) / (350)) + 1;
var _start = <%= lastID %>;
var _end = <%= lastID %> + _n - 1;
$scope.getDataset = function() {
fb.orderByChild('id').startAt(_start).endAt(_end).limitToLast(_n).on("child_added", function(dataSnapshot) {
$scope.data.push(dataSnapshot.val());
$scope.$apply()
console.log("THE VALUE:"+$scope.data);
});
_start = _start + _n;
_end = _end + _n;
};
$scope.getDataset();
window.addEventListener('scroll', function() {
if (window.scrollY === document.body.scrollHeight - window.innerHeight) {
$scope.$apply($scope.getDataset());
}
});
});
// Compile the whole <body> with the angular module named "app"
angular.bootstrap(document.body, ['app']);
</script>
SITUATION:
lastID is the ID of the last post created. They go backwards (from -1 to lastID).
This solution works perfectly except when some posts are deleted.
(Posts are ordered from latest to oldest (from lastID to -1)).
For example, if there are 16 posts, lastID = -16.
If I delete posts -13 to -5 and post -3, lastID stays at -16.
Which means that when the page loads, no posts appear after the 3 first posts (-16 to -14) are loaded and I need to scroll repeatedly down to get post -4 to appear.
QUESTION:
How can I make sure that if some posts are deleted, my Infinite Scroll script will skip the ids of the inexisting posts and just load the 3 closest posts ?
WHAT I CHECKED:
I looked at this:
Display posts in descending posted order
But I am not sure as to how I would implement those solutions inside my infinite scroll. Any ideas ?