Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ngInfiniteScroll retrieve data by createdAt Descending

I'm using firebase to save posts that have the following data:

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

What I'm trying to achieve is to get the most recent posts first and then load the older ones while the user scrolls down.

With posts retrieved using ngInfiniteScroll I'm being able to order desc using <div ng-repeat="post in posts | orderBy:'-createdAt'"> but ngInfiniteScroll keep returning the old posts first. I'm ordering but i'm ordering the older ones.

I already tried using the same logic ("-createdAt") in ngInfiniteScroll but it was not effective.


My js is pretty much this:

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

Security and rules:

"posts": {
          ".read": true,
          ".indexOn": "createdAt",
          "$post": {
            ".read": true,
            ".write": true,
            "$other": {
               ".validate": true
            }
         }
      }
like image 475
adolfosrs Avatar asked Oct 18 '22 12:10

adolfosrs


1 Answers

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.

like image 174
garrettmac Avatar answered Nov 12 '22 15:11

garrettmac