0

I have attempted to access a child from a syncronized array after it has been added like so:

var postsRef = new Firebase("https://bragr.firebaseio.com/posts");
var ref = new Firebase("https://bragr.firebaseio.com");
var auth = $firebaseAuth(ref);
$scope.array.$loaded().then(function() {
    // NEW POST IS ADDED:
    postsRef.on('child_added', function(childSnapshot, prevChildKey) {
         var index = $scope.posts.$indexFor(id);
         var tmpPost = $scope.posts[index];
         console.log(tmpPost); // LOGS -1
    });
});

It seems that when a new Post is added, the index returns -1 which means that the item is not yet in the list. I assumed the child_added function was called when the child was added to said list. But it seems I am mistaken.

How can I ensure the Item is accessible from the list after it has been added? I am using Angular Fire, and I am attempting to update the view when a new Item is added by another user.

user3024235
  • 415
  • 1
  • 5
  • 16

1 Answers1

0

To respond to new items being added to $firebaseArray(), you need to extend it and override $$added() as documented here: https://www.firebase.com/docs/web/libraries/angular/guide/extending-services.html#section-firebasearray

A few relevant questions:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807