【发布时间】:2017-05-10 10:45:36
【问题描述】:
代码:
<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>
情况:
lastID 是最后创建的帖子的 ID。它们倒退(从 -1 到 lastID)。
此解决方案完美运行,除非删除某些帖子。
(帖子按从最新到最旧的顺序排列(从 lastID 到 -1))。
例如,如果有 16 个帖子,lastID = -16。
如果我删除帖子 -13 到 -5 和帖子 -3,lastID 将保持在 -16。
这意味着当页面加载时,在加载前 3 个帖子(-16 到 -14)后没有帖子出现,我需要反复向下滚动才能显示帖子 -4。
问题:
我如何确保如果某些帖子被删除,我的无限滚动脚本将跳过不存在的帖子的 id 并仅加载 3 个最近的帖子?
我检查了什么:
我看了这个:
Display posts in descending posted order
但我不确定如何在无限滚动中实现这些解决方案。有什么想法吗?
【问题讨论】:
标签: javascript node.js firebase firebase-realtime-database