In my customer-detail component I have the following code that achieves what I'm after but not in the reactive/observable way I think might be possible.
Instead of wrapping this.isLoading = true;
in an if statement, is there a way to do this using reactive programming techniques? Perhaps by cancelling/dropping the delayed observable if the customer is retrieved first? Or, am I going about this the wrong way?
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);
}
}