Как я могу реализовать событие child_removed с angularfire2 +

Я тестирую карты Google с помощью firebase + angularfire2.

Структура данных Firebase

{
  markers: 
      key1 : {lat:1, lng:2}
      key2 : {lat:3, lng:4}           
}

с JS + firebase все 3 события работают хорошо.

var markers = database.ref('markers');
markers.on('child_added',  function(snapshot) {
    addMarkerUI(snapshot);
});  
markers.on('child_changed', function(snapshot){
    updateMarkerUI(snapshot);
});
markers.on('child_removed', function(snapshot, prevChildKey) {
    removeMarkerUI(snapshot);
});  

Но с angularfire2 он вел себя совсем иначе.

itemsRef: AngularFireList<any>;

constructor(private db: AngularFireDatabase) { }

ngOnInit() {
    this.itemsRef = this.db.list('markers');
    this.itemsRef.snapshotChanges(['child_added', 'child_changed', 'child_removed'])
        .subscribe(actions => {
            console.log(actions);
            actions.forEach(action => {
                if (action.type === 'child_added') {// works
                    console.log(action)
                    console.log(action.type);
                    console.log(action.key);
                }

                if (action.type === 'child_changed') {// works
                    console.log(action)
                    console.log(action.type);
                    console.log(action.key);
                }

                if (action.type === 'child_removed') {// does not works
                    console.log(action)
                    console.log(action.type);
                    console.log(action.key);
                }
                // this.items.push(action.payload.val());
                // console.log(action.payload.val());
            });
        });

Событие child_removed просто возвращает действия без удаленного дочернего элемента. Как лучше всего реализовать "child_removed" для метода removeMarkerUI?


person John    schedule 14.03.2018    source источник
comment
Похоже, это может быть ошибка, можете ли вы создать репродукцию на StackBlitz и опубликовать ее в наших выпусках Github?   -  person James Daniels    schedule 14.03.2018
comment
@JamesDaniels Я создал это здесь, stackblitz.com/edit/angular-um7hbv см. Консоль. Я тоже сообщу об этом на Github.   -  person John    schedule 15.03.2018


Ответы (1)


используйте stateChanges, а не snapshotChanges

items: Observable<any[]>;
itemsRef: AngularFireList<any>;

constructor(private db: AngularFireDatabase) {}

ngOnInit() {
    this.items$ = this.db.list('markers');
    this.items$.stateChanges(['child_added', 'child_changed', 'child_removed'])
      .subscribe(action => {
        if (action.type === 'child_added') {
          console.log('I was added', action, action.payload.val())
        }

        if (action.type === 'child_changed') {
          console.log('I was modified', action, action.payload.val())
        }

        if (action.type === 'child_removed') {
          console.log('I was deleted', action, action.payload.val())
        }

      });
}
person John    schedule 16.03.2018