2016-11-14 130 views
0

我有以下代碼:與rxjs處理可觀測量邏輯

Observable.combineLatest(
    this.data.getPersonalData(myfilter), 
    this.peopleService.selectedPerson, 
).subscribe(([data, person]) => { 
    this._data = data; 
    if (this._data.length) { 
     this.infoService.getInfo(
      person.id 
     ) 
     .subscribe(res => { 
      this.handleInfo(res); 
     }); 
    } 
}); 

Observable.combineLatest(
    this.data.getPersonalData(myfilter), 
    this.sectionService.selectedSection 
).subscribe(([data, section]) => { 
    this.filterbySection(s); 
}); 

我會盡量解釋的場景:

首先,我們得通過myfilter給所有可用的personaldata。 Okey,所以現在,給定一個selectedPerson我們獲得一個人的所有信息,並且handleInfo將這個信息關聯到data中的一組對象。但事情是,當它結束時,我想要filterbySection當我有一個selectedSection

我知道我可以把this.sectionService.selectedSection第一combineLatest,但每次用戶更改部分,它將運行infoService要去數據庫,並把所有的信息,我不需要這種行爲。當我選擇一個人時,我需要運行getInfo,處理此信息將其映射到_data,然後按部分過濾所有「info + data」,每次用戶單擊一個新節。

回答

0

我不確定這個解決方案。但我可以創造一個EventEmitterinfo,所以後來我可以訂閱personalInfo,並在第二combineLastest我會是這樣的:

Observable.combineLatest(
    this.infoService.selectedInfo, 
    this.sectionService.selectedSection 
).subscribe(([info, section]) => {this.filterBySection(info, section)});