假设我正在使用firebase作为评论系统,我想检索给定主题的评论,但是在一个主题中有很多评论我不想一次全部检索它们.我还希望最新的评论显示在最上面.
似乎以相反的顺序显示firebase记录的唯一方法是将它们全部检索,然后反过来迭代它们.
这可能会对大型数据集变得非常笨重,特别是对于移动客户端.
有没有更好的方法?从Firebase查询分页数据的升级或降序的一般和首选解决方案是什么?
我做了什么(做得不正确):
码:
<script>
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function ($scope, $firebaseArray, $timeout) {
$scope.data = [];
var _n = Math.ceil(($(window).height() - 50) / (350)) + 1;
var start = 0;
var end = _n - 1;
var lastScore = <%=lastScore%>;
console.log("FIRST FIRST FIRST LAST SCORE:" + lastScore);
var firstElementsLoaded = false;
$scope.getDataset = function() {
fb.orderByChild('score').endAt(lastScore).limitToLast(_n).on("child_added", function(dataSnapshot) {
lastScore = dataSnapshot.child("score").val() - 1;
console.log("LAST TOP LIKED:"+ lastScore);
$scope.data.push(dataSnapshot.val());
$scope.$apply();
console.log("THE VALUE:"+$scope.data);
$scope.data.splice(start, end).concat($scope.data.reverse());
$scope.$apply();
start = start + _n; …Run Code Online (Sandbox Code Playgroud)