2017-10-11 144 views
-1

我不是rxjs的專家。關於相同功能的開關圖

我有一個方法,實際上保存。

this.saveProducts(this.boxes, 'BOXES') 

但還有另外兩種不同類型的項目需要使用相同的方法進行保存,只是參數不同而已。

this.saveProducts(this.containers, 'CONTAINER') 

在我的組件中,我幾乎沒有其他的獨立存儲正在發生,所有這些都應該發生一個一個。

所以我的方法看起來像這樣。

return this.service.edit(materials) 
     .do((data) => { 
      this.materials= data; 
     }) 
     .switchMap(() => this.customerService.addCustomer(this.id, this.customers)) 
     .switchMap(() => this.saveProducts(this.boxes, 'BOXES')) 
     .switchMap(() => this.saveProducts(this.containers, 'CONTAINER')) 
     .switchMap(() => this.saveProducts(this.books, 'BOOKS')) 
     .do(() => { 
     ....... 
      } 

但是發生了什麼是它永遠不會調用第二saveProducts方法,除非我從第一個返回

private saveProducts(products: IProduct[], type: 
Type): Observable<any> { 
     console.log(type); 
    } 

回答

0

感謝的人誰看了一下.. 回答這個問題是如果沒有任何東西需要保存,則返回空的可觀察對象。

if (products$.length === 0) { 
     return Observable.of([]); 
    } 

謝謝你們..快樂編碼..