My goal is to use RxJs switchMap
in an Ionic 3 project, to cancel any ongoing login attempts if a server is offline, and use latest request only. Otherwise I can get some unwanted asynchronous side effects later.
In my view layer, I'm using submitStream$
to capture a button clickstream, then passing that to the service layer as a method argument.
In the service layer, I'm creating response$
via a switchMap
, combining, the passed parameter submitStream$
, with an Observable<Response>
that comes back from this.webServiceUtil.testLogin()
.
I tried implementing this code:
HTML markup
<button #submit type="submit">Submit</button>
Typescript : View Layer
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
import { ElementRef, ViewChild } from '@angular/core';
export class LoginPage {
@ViewChild('submit') button: ElementRef
submitStream$:any;
...
ionViewDidLoad() {
this.submitStream$ =
Observable.fromEvent(this.button.nativeElement, 'click')
}
...
}
Typescript : Service Layer (submitStream$ comes in as method argument)
this.response$ = submitStream$.switchMap(click =>
this.webServiceUtil.testLogin())
I've also tried creating this.submitStream$
in ngAfterViewInit()
When things didn't work I also tried appending this to the Observable.fromEvent:
.subscribe(ev => {
console.log('LoginPage: ionViewDidLoad() submitStream$.subscribe(): ev:', ev);}
)
How do I overcome the 'ERROR TypeError: Invalid event target'?