Skip to content Skip to sidebar Skip to footer

How Is This Firebase List Displayed In Order In The Browser, Even Though Js Objects Are Unordered?

As far as I understand, JavaScript objects are an unordered collection of properties, whereas Firebase lists use IDs as property keys, which the docs say are chronologically ordere

Solution 1:

Perhaps you can utilize the setPriority API to order your data. I believe with setPriority, children are sorted based on this priority using the following rules:

  1. Children with no priority come first.
  2. Children with a number as their priority come next. They are sorted numerically by priority (small to large).
  3. Children with a string as their priority come last. They are sorted lexicographically by priority.
  4. Whenever two children have the same priority (including no priority), they are sorted by
  5. name. Numeric names come first (sorted numerically), followed by the remaining names (sorted lexicographically).

So if you set the priority numerically for all your children, you can expect your set to behave in an ordered manner. Check docs on setPriority [here][https://www.firebase.com/docs/javascript/firebase/setpriority.html]

Solution 2:

In general, objects are ordered correctly. There are only a couple of notable exceptions, like chrome's inexplicable handling of numeric keys. So your data appears correct because as long as you haven't run afoul of any of chrome's thick skull.

To ensure proper ordering when working with raw Firebase data, you can utilize forEach:

firebaseRef.once('value', function(snap) {
   snap.forEach(function(ss) {
      console.log(ss.name()); // prints names in guaranteed order
   });
});   

The child_added events provide a prevChild that helps you order incoming values in your UI:

firebaseRef.on('child_added', function(snap, prevChild) {
   console.log('put ' + snap.name() + ' after ' + prevChild);
});

AnglularFire provides special tools for dealing with this. In the upcoming 0.8 release (releasing in a couple weeks), there will be a $asArray() method for getting sorted results in guaranteed order.

$scope.messages = $firebase(ref).$asArray();

And in the current iteration, you can utilize the orderByPriority filter (less efficient, but effective):

<ling-repeat="message in messages | orderByPriority">{{message|json}}</li>

Post a Comment for "How Is This Firebase List Displayed In Order In The Browser, Even Though Js Objects Are Unordered?"