5

我正在使用 firebase 保存具有以下数据的帖子:

createdAt: "Sun Apr 03 2016 18:32:46 GMT-0300 (BRT)"

我想要实现的是首先获取最新的帖子,然后在用户向下滚动时加载较旧的帖子。

使用 ngInfiniteScroll 检索帖子后,我可以使用 desc 订购,<div ng-repeat="post in posts | orderBy:'-createdAt'">但 ngInfiniteScroll 会继续首先返回旧帖子。我正在订购,但我订购的是旧的。

我已经尝试在 ngInfiniteScroll 中使用相同的逻辑 ( "-createdAt") 但它没有效果。


我的js差不多是这样的:

  var baseRef = new Firebase(FBURL).child("posts");
  var scrollRef = new Firebase.util.Scroll(baseRef, "createdAt");
  $scope.posts = $firebaseArray(scrollRef);
  $scope.posts.scroll = scrollRef.scroll;

安全和规则:

"posts": {
          ".read": true,
          ".indexOn": "createdAt",
          "$post": {
            ".read": true,
            ".write": true,
            "$other": {
               ".validate": true
            }
         }
      }
4

2 回答 2

2

Looks like you found your solution but you were on the right track piping with the orderBy but just try tweaking it a little. Try using orderBy:'+':true", where orderBy this says you want to order it by something and the +:true says order it where anything new is on top, where -:true would say order new content on the bottom. But you can look the angular docs for it here. So if where handling it on the HTML side it would look something like this ng-repeat="post in posts| orderBy:'+':true" or:

    <div ng-repeat="post in posts| orderBy:'+':true">
           [....post info here]
      </div>

But give that a try, hope it helps.

于 2016-04-12T21:22:43.787 回答
1

在深入研究了可用的文档后,我可以在这里找到一个丑陋的解决方法。

基本上是使用负时间戳并正常检索createdAt(升序)。

因此,在保存数据时,我正在执行以下操作:

{
  createdAt: Firebase.ServerValue.TIMESTAMP,
  createdAtDesc: 0 - Date.now()
}

仍在寻找更好的解决方案。

于 2016-04-07T01:29:51.060 回答