2017-07-19 73 views
0

我有一組,我嘗試按順序發送彼此的帖子,是這樣的:Angular2順序POST調用

if (this.ifoForm.valid) { 
    if (this.otherCheck && (!this.selectedLang || !this.selectedInterp)) { 
     swal('Error!', 'Please choose Language and Interpreter!', 'error'); 
    } else { 
     // create CallTracker entity 
     this.createCallLog(); 
     // create CTClient entity 
     this.createCTClient(); 
     // create CTTeln entity 
     this.createCTTelns(); 
     // create CTClientOffence entities 
     this.createCTClientOffences(); 
    } 
} 

的事情是,CTClient無法創建瓦特/ OA CallTracker(呼叫記錄)實體,與CTTelns相同。 CTClientOffences也不能創建,直到CallTrackerCTClient實體存在。

我在實例化的我的容器組件實體對象時的職位返回:

private callLog: CallTracker; 
private logClient: CTClient; 
private logTelns: CTTeln[]; 
private logCharges: CTClientOffence[]; 

例如:

public onLogNotify(callLog): void { 
    // add new CallTracker entity to database 
    this._callTrackerService.addLog(callLog) 
     .subscribe(
      res => this.callLog = res, 
      err => console.log(err) 
     ); 
} 

我的問題是:我可以使用這些對象來限制通話到後續的POSTS,直到適當的對象被實例化?即什麼我可以使用,而不是.timeout()

public onClientNotify(client): void { 
    // add new CTClient entity to database 
    this._ctClientService.addCTClient(client) 
     .timeout(2000) // wait for CallTracker entity to be made 
     .subscribe(
      res => this.logClient = res, 
      err => console.log(err) 
     ); 
} 
+0

我認爲這是你在找什麼https://stackoverflow.com/documentation/rxjs/8247/common-recipes/28035 /發送多重連續http請求#t = 201707191342561190654 – martin

+0

@馬丁謝謝你,這似乎適用,但我仍然是非常新的rxjs和我有麻煩概念化解決方案;你能用我的代碼提供一個例子嗎?我目前每個實體都有自己的服務。 – Milo

回答

0

我想通了使用這種模式:Angular2 - Multiple dependent sequential http api calls

呈現組件:

if (this.ifoForm.valid) { 
    if (this.otherCheck && (!this.selectedLang || !this.selectedInterp)) { 
     swal('Error!', 'Please choose Language and Interpreter!', 'error'); 
    } else { 
     // perform POSTs 
     this.doPostCalls(); 
    } 
} 

@Output() dataNotify: EventEmitter<any> = new EventEmitter<any>(); 
// CREATE (POST) DATABASE ENTITIES 
private doPostCalls(): void { 
    // execute functions 
    this.createCallLog(); 
    this.createCTClient(); 
    this.createCTTelns(); 
    this.createCTClientOffences(); 
    // send data to parent component for http calls 
    this.dataNotify.emit({ 
     callLog: this.newCallLog, 
     client: this.newLogClient, 
     telns: this.telnData, 
     charges: this.chargesData 
    }); 
} 

容器組件:

public onDataNotify(data): void { 
    this.callLog = data.callLog; 
    this.logClient = data.client; 
    this.logTelns = data.telns; 
    this.logCharges = data.charges; 
    if (this.callLog != undefined) { 
     this.createCallTrackerEntity(); 
    } 
} 

public createCallTrackerEntity(): void { 
    console.log("In: onLogNotify(data)"); 
    // add new CallTracker entity to database 
    this._callTrackerService.addLog(this.callLog) 
     .subscribe(
     res => console.log("CallTracker Data Added."), 
     err => console.log(err), 
     () => this.createCTClientEntity() 
     ); 
} 

public createCTClientEntity(): void { 
    console.log("In: onClientNotify(client)"); 
    // add new CTClient entity to database 
    this._ctClientService.addCTClient(this.logClient) 
     .subscribe(
     res => console.log("CTClient Data Added."), 
     err => console.log(err), 
     () => this.createCTTelnEntities() 
     ); 
} 

public createCTTelnEntities(): void { 
    console.log("In: onTelnNoify(telns)"); 
    // add new CTTeln entity to database 
    this._ctTelnService.addCTTeln(this.logNumber, this.logTelns) 
     .subscribe(
     res => console.log("CTTeln Data Added."), 
     err => console.log(err), 
     () => this.createCTClientOffenceEntities() 
     ); 
} 

public createCTClientOffenceEntities(): void { 
    console.log("In: onOffenceNotify(offences)"); 
    // add mew CTClientOffence entity to database 
    this._ctClientOffenceService.addCTClientOffence(this.logNumber, this.maxClientId, this.logCharges) 
     .subscribe(
     res => console.log("CTClientOffence Data Added."), 
     err => console.log(err) 
     ); 

}