2017-07-19 3 views
0

ich eine Reihe von Stellen, die ich in Folge von einer anderen, so etwas wie zu senden versuchen:Angular2 Sequential POST Anrufe

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(); 
    } 
} 

Die Sache ist die, dass CTClient kann nicht w/oa CallTracker erstellt werden (Anrufprotokoll) Einheit, gleich CTTelns. Auch CTClientOffences kann nicht erstellt werden bis CallTracker und CTClient Entitäten vorhanden sind.

ich in meinem Container-Komponente Entitätsobjekten, die instanziiert werden, wenn die Pfosten zurück:

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

Zum Beispiel:

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

Meine Frage ist: kann ich diese Objekte verwenden, um den Anruf zu beschränken zu den nachfolgenden POSTS, bis die entsprechenden Objekte instanziiert werden? das heißt, was kann ich anstelle von .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

Ich denke, das ist das, was Sie suchen https://stackoverflow.com/documentation/rxjs/8247/common-recipes/28035/sending-multiple-sequential-http-Anfragen # t = 201707191342561190654 – martin

+0

@martin Vielen Dank, dies scheint anwendbar, aber ich bin immer noch sehr neu zu rxjs und habe Probleme bei der Konzeption einer Lösung; Könnten Sie ein Beispiel mit meinem Code angeben? Ich habe derzeit jedes Unternehmen unter seinem eigenen Dienst. – Milo

Antwort

0

ich es herausgefunden, dieses Muster mit: Angular2 - Multiple dependent sequential http api calls.

Präsentation Komponente:

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 
    }); 
} 

Container-Komponente:

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) 
     ); 

}