sonicblis
sonicblis

Reputation: 3194

How do you sort by priority when ordering by child in Firebase?

I basically want to do this:

var ref = new Firebase("[myapp].firebaseio.com");
var things = ref.child("things");
var thingsQuery = things.orderByPriority().orderByChild("owner").equalTo("[ownerKey]")
var angularReadyThings = $firebaseArray(thingsQuery);

I'm using Firebase's query capabilities orderByChild() and equalTo() to get a subset of data and creating a $firebaseArray from that query to display a part of a list of things under a specific node. The user can drag and drop the list to set $priority on each item, and I can save the priority correctly, but the data doesn't come back ordered by priority (which I guess makes sense). I can't use orderByPriority() along with orderByChild() like I have it above on line 3 (multiple orderBys aren't supported). Using an orderBy:'$priority' filter on my ng-repeat (I'm using angular for this) works until I drag/drop sort the list, at which point the items revert to their retrieved (unordered) state.

Is there a way to use orderByChild() and equalTo() and get the results ordered by priority from Firebase?

Upvotes: 0

Views: 960

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

Firebase can only sort/filter by a single condition, whether that is key, a child property or priority.

It seems like you only want to filter by a child property and then order by priority. In that case you can do the ref.orderByChild("owner").equalTo(ownerKey) with Firebase querying and do the re-ordering client-side

Side note: there is little reason to still use Firebase's old priorities for ordering. It is way clearer to just add a named child property with the same value.

Upvotes: 2

Related Questions