How to make ngInfiniteScroll get data on createdAt Descending

I use firebase to save messages that have the following data:

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

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

With messages received using ngInfiniteScroll, I can order desc using <div ng-repeat="post in posts | orderBy:'-createdAt'"> , but ngInfiniteScroll first returns the old messages. I order, but I order the elders.

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


My js pretty much:

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

Safety and rules:

 "posts": { ".read": true, ".indexOn": "createdAt", "$post": { ".read": true, ".write": true, "$other": { ".validate": true } } } 
+5
source share
2 answers

It sounds like you found your solution, but you were on the right track with orderBy , but just try orderBy it a bit. Try using orderBy:'+':true" , where orderBy says that you want to order it with something, and +:true says that it orders it, where something new is on top, where -:true will say to order new content below. But you can see the angular documents for here . Therefore, if you process it on the HTML side, it will look something like this: ng-repeat="post in posts| orderBy:'+':true" ng-repeat="post in posts| orderBy:'+':true" or:

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

But try, hopefully this helps.

+2
source

Looking a little deeper at the available documents, I found an ugly workaround here .

Basically use a negative timestamp and retrieve createdAt normally (ascending).

Therefore, when saving data, I do the following:

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

We are looking for the best solution.

+1
source

Source: https://habr.com/ru/post/1246364/


All Articles