Here is a class that can help you. This is a little ugly and not tested, but it should work.
import { Observable } from 'rxjs'; class ObservableFirebaseListWithTempStorage { private observer; private tempItems: Array<any>; private dbItems: Array<any>; public items: Observable<Array<any>>; constructor(private $ref, private mapFunction) { this.items = Observable.create(observer => { this.observer = observer; }); $ref.subscribe(snapshot => { this.dbItems = this.mapFunction(snapshot); this.updateItems(); }); } private updateItems(){ let tempList = []; tempList.push(this.dbItems); tempList.push(this.tempItems); this.observer.next(tempList) } public addItem(item): void { this.tempItems.push(item); this.updateItems(); } public saveItems():void{ this.tempItems.forEach(item=>{ this.$ref.push(item); }); }; }
You have to change your map2itemList () a bit, so it takes a snapshot as a parameter and returns the displayed items, rather than setting something.
Now you can instantiate this class:
let items = new ObservableFirebaseListWithTempStorage(this.$ref, this.map2itemList); this.itemList = items.items;
Add an item using:
this.items.addItem(yourNewTempItem);
and save the elements in db:
this.items.saveItems();
source share