Within my customer-detail component, I have implemented code that achieves the desired outcome. However, I believe there might be a more reactive and observable way to approach this task.
Instead of using an if statement to set this.isLoading = true;
, is there a method to accomplish this utilizing reactive programming techniques? For instance, by terminating or disregarding the delayed observable if the customer data is fetched first? Or could there be a different approach altogether?
export class CustomerDetailComponent implements OnInit {
customer: Customer;
errorMessage: string;
isLoading: boolean;
constructor(
private customerService: CustomerService,
private route: ActivatedRoute,
private router: Router,
private location: Location
) { }
ngOnInit() {
let idParam = this.route.params
.distinctUntilChanged(params => params['id']);
idParam.subscribe(params =>
{
this.errorMessage = '';
});
idParam.delay(300).subscribe(params =>
{
if (!(this.customer && this.customer.id == params['id']))
this.isLoading = true;
});
idParam.switchMap((params: Params) => this.customerService.getCustomer(params['id']))
.subscribe(customer =>
{
this.customer = customer;
this.isLoading = false;
},
error => this.errorMessage = error);
}
}