I am currently utilizing Angular 2 with TypeScript to create a web application that relies on HTTP calls to interact with a REST Server.
One issue I am facing is the inconsistent speed of GET calls, requiring me to add delays before some calls. How can I address this problem effectively and eliminate the need for such delays?
For instance, after deleting a Manufacturer (as shown in the code below), I need to return to the view displaying all Manufacturers (where I implement the GET call within ngOnInit()).
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { DataService } from '../../../data/data.service';
import { CallManufacturerServices } from '../calls/calls.service';
import { ManufacturerClass } from '../class/manufacturer.class';
@Component({
templateUrl: './app/inventory/manufacturer/view/view.component.html',
styleUrls: ['./app/inventory/manufacturer/view/view.component.css'],
providers: [CallManufacturerServices]
})
export class ViewComponent implements OnInit, OnDestroy {
private sub: any;
private ServerData: ManufacturerClass = {
ManufacturerId: 0,
ManufacturerName: 'N/D',
ManufacturerWebSite: 'N/D'
};
constructor(
private route: ActivatedRoute,
private router: Router,
private data: DataService,
private calls: CallManufacturerServices) {
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = +params['id'];
this.getSingleManufacturer(id);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
private getSingleManufacturer(id: number) {
this.calls.GetSingleManufacturer(id).subscribe(
(data) => {
this.ServerData = data.json();
},
error => console.log(error),
() => console.log('getSingleManufacturer complete!')
);
}
private deleteManufacturer(_manufacturer: ManufacturerClass) {
this.calls.DeleteManufacturer(_manufacturer.ManufacturerId).subscribe(
error => console.log(error),
() => console.log('deleteManufacturer complete!')
);
/* This kind of delay method does not work */
setTimeout(() => {}, 2000); // Add the delay here
/* The Get call will be initiated when navigating back to Manufacturer */
this.goToManufacturer();
}
private goToManufacturer() {
this.router.navigate(['/inventory/manufacturer']);
}
private goToEdit(manufacturer: ManufacturerClass) {
this.router.navigate(['/inventory/manufacturer/edit', manufacturer.ManufacturerId]);
}
}