簡體   English   中英

角度| 訂閱多個可觀察物

[英]Angular | Subscribe to multiple observables

我正在嘗試創建用戶要去的事件列表。 首先,我獲取事件密鑰,然后我想做的是訂閱每個事件並監聽更改。 當前只有最后一個事件起作用,因為this.eventRef在for循環中已更改。

eventRef: AngularFireObject<any>


getEvents() {

const eventsGuestsLookup = this.db.object(`eventsGuestsLookup/${this.uid}`).valueChanges()

this.eventsGuestsLookupSub = eventsGuestsLookup
  .subscribe(eventKeys => {

    if (eventKeys) {
      console.log(eventKeys)
      for (const k in eventKeys) {
        if (eventKey.hasOwnProperty(k)) {

          this.eventRef = this.db.object(`events/${k}`)

          console.log(this.eventRef)

          this.eventRef.snapshotChanges().subscribe(action => {

            const key = action.payload.key
            const event = { key, ...action.payload.val() }

           this.makeEvents(event)

          })
        }
      }
    }
  })

}

接下來,我要做的是獲取用戶的響應,對於每種狀態,我想顯示某些信息。 我不知道這樣做的其他方法,因此我同時檢查了attending列表和notAttending attending列表,並且如果用戶有響應,我會更改事件屬性。

makeEvents(event) {
console.log(event)

event.goingText = "RSVP"
event.setGoing = 'rsvp'
event.setColor = "rsvp-color"

const attending = this.db.object(`attendingLookup/${this.uid}/${event.key}`).valueChanges()

this.attendingLookupSub = attending
  .subscribe(data => {
    console.log('attending', data)
    if (data) {
      event.goingText = "ATTENDING"
      event.setGoing = 'thumbs-up'
      event.setColor = 'attending-color'
    }
  })

const notAttending = this.db.object(`not_attendingLookup/${this.uid}/${event.key}`).valueChanges()

this.notAttendingLookupSub = notAttending
  .subscribe(data => {
    console.log('not attending', data)
    if (data) {
      event.goingText = "NOT ATTENDING"
      event.setGoing = 'thumbs-down'
      event.setColor = 'not-attending-color'
    }
  })

this.events.push(event)

}

***編輯

const eventsGuestsLookup = this.db.object(`eventsGuestsLookup/${this.uid}`).valueChanges()
eventsGuestsLookup.subscribe(keys => {
  of(keys).pipe(
    mergeMap(keys => {
      Object.keys(keys).map(k => {
        console.log(k)
      })
      return merge(Object.keys(keys).map(k => this.db.object(`events/${k}`)))
    })
  ).subscribe(data => console.log('data', data))
})

您要實現的目標與您的可觀測數據集保持一致。 要實現它,您可以執行以下操作:

//Dummy eventKeys observable.
const obs1$ = new BehaviorSubject({key:1, action: 'lorem'});
const obs2$ = new BehaviorSubject({key:2, action: 'lorem'});
const obs3$ = new BehaviorSubject({key:3, action: 'lorem'});

const eventKeys = {
  obs1$,
  obs2$,
  obs3$
};
// Dummy eventsGuestsLookup observable.
of(eventKeys)
.pipe(
  //eventsGuestsLookup dispatch collection of obserbable, we want to flat it.
  mergeMap(ev => {
    // We merge all observables in new one.
    return merge(...Object.keys(ev).map(k => ev[k]));
  }),
).subscribe(console.log);

重要說明ev[k]是一個Observable對象。 對於您的情況,您應該執行以下操作:

.map(k => this.db.object(`events/${k}`)) // will return observable.

現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM