Last active
March 27, 2019 18:41
-
-
Save angulartist/63a5c2f58b3fa8c74977bc450ae05fd8 to your computer and use it in GitHub Desktop.
Reactive Firebase / BASICS - RxFire, Getting documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Getting a single firestore document */ | |
import { docData } from 'rxfire/firestore' | |
... | |
someFunction() { | |
const todoRef = db.doc('todos/myTodo') | |
const todo$ = docData(todoRef, 'id') | |
todo$.subscribe(todo => /* doSomething() */) | |
} | |
/* Getting a firestore collection */ | |
import { collectionData } from 'rxfire/firestore' | |
... | |
someFunction() { | |
const todosRef = db.collection('todos') | |
const todos$ = collectionData(todoRef, 'id') | |
todos$.subscribe(todos => /* doSomething() */) | |
} | |
/** | |
* Preventing memory leaks | |
* (make a component instance available for garbage collection) | |
*/ | |
import { takeUntil } from 'rxjs/operators' | |
... | |
destroy$: Subject<boolean> = new Subject<boolean>() | |
... | |
componentDidUnload() { // or whatever your unload hook is. | |
this.destroy$.next(true) | |
this.destroy$.unsubscribe() | |
} | |
... | |
someFunction() { | |
const todosRef = db.collection('todos') | |
const todos$ = collectionData(todoRef, 'id') | |
todos$ | |
// unsub when destroy$ obs emits | |
.pipe(takeUntil(this.destroy$)) | |
.subscribe(todos => /* doSomething() */) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment