googlearchive / firebase-util

An experimental toolset for Firebase
http://firebase.github.io/firebase-util
MIT License
276 stars 67 forks source link

Passing Query into Firebase.util.Scroll to reverse order #25

Closed rosslavery closed 9 years ago

rosslavery commented 9 years ago

Thanks for the great work on the latest release.

I'm looking to use the Scroll class on a collection of comments, where they each have a timestamped createdAt field. The items need to be ordered with the most recent at the top. I can pass createdAt to the Scroll class, but it uses orderByChild which orders them with the oldest first.

Scroll seems to only lets you pass a raw firebase reference to it, not a Query, so I can't use limitToLast() to reverse the order that the items are loaded in.

When using limitToLast on the scrollRef, it throws:

limitToLast is not supported for Paginate and Scroll references. Try calling it on the original reference used to create the instance instead.

But when using limitToLast on the original reference as indicated, it throws:

First argument to Firebase.util.Scroll must be a valid Firebase ref

Reversing the collection on the client doesn't seem like a possibility because Scroll is loading comments in via "pages" from the opposite end of the collection. Hopefully that makes sense...

I understand Scroll is using Queries internally, so passing it a query would break it since chaining Queries isn't possible, but is there an optimal strategy for this?

katowulf commented 9 years ago

limitToLast() doesn't actually reverse the order of the elements, it simply fetches the last x items. If that's the goal, then Scroll isn't necessary, as it's designed to work with an unspecified number of items (quasi-infinite lists).

Regarding actual reversal: this is a limitation of the queries API; until one can reverse the items in the query, Firebase.util.Scroll won't be able to help here.

rosslavery commented 9 years ago

Got it, thanks for the response Kato.

I'll close this now, hopefully the queries API gets that feature in a future release!