zoukankan      html  css  js  c++  java
  • [AngularFire2 & Firestore] Example for collection and doc

    import {Injectable} from '@angular/core';
    import {Skill} from '../models/skills';
    import {AuthService} from '../../auth/services/auth.service';
    
    import 'rxjs/add/operator/do';
    import 'rxjs/add/operator/startWith';
    import 'rxjs/add/operator/catch';
    import {Observable} from 'rxjs/Observable';
    import {AngularFirestore, AngularFirestoreCollection} from 'angularfire2/firestore';
    
    
    @Injectable()
    export class SkillsService {
    
      skillCollection: AngularFirestoreCollection<Skill>;
      skills: Observable<Skill[]>;
      events: Observable<any>;
    
      constructor(private afs: AngularFirestore,
                  private authService: AuthService) {
    
        this.skillCollection = afs.collection(`users/${this.uid}/skills`,
          ref => ref.orderBy('name').limit(10));
        this.events = this.skillCollection.auditTrail();
      }
    
      get uid() {
        return this.authService.user.uid;
      }
    
      getSkills(): Observable<Skill[]> {
        return this.skillCollection
          .snapshotChanges()
          .startWith([]) // To solve the snapshotChange doesn't fire for empty data
          .map((actions) =>
            actions.map(({payload}) => ({id: payload.doc.id, ...payload.doc.data()} as Skill))
          );
      }
    
      getSkill(key): Observable<Skill> {
        return this.afs.doc(`users/${this.uid}/skills/${key}`)
          .snapshotChanges()
          .map(({payload}) => ({id: payload.id, ...payload.data()} as Skill));
      }
    
      addSkill(skill: Skill) {
        return this.skillCollection.add(skill);
      }
    
      updateSkill(key: string, skill: Partial<Skill>) {
        return this.afs.doc<Skill>(`users/${this.uid}/skills/${key}`).update(skill);
      }
    
      removeSkill(key: string) {
        return this.afs.doc<Skill>(`users/${this.uid}/skills/${key}`).delete();
      }
    
    }
  • 相关阅读:
    Linux下nginx 的常用命令
    Mybatis generator 自动生成代码(2)
    Android Retrofit2 网路编程
    Android webView输出自定义网页
    Android Studio OkHttpClient使用
    Android Studio SVN使用
    Android Toolbar的使用 顶部标题栏+后退键
    Android DrawLayout + ListView 的使用(一)
    RabbitMQ配置与安装
    Struts2拦截器
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7770409.html
Copyright © 2011-2022 走看看