Search code examples
angularrxjsng-bootstrapbootstrap-typeahead

Angular typeahead with RxJs switchMap subscribe


Using Angular bootstrap Typeahead plugin to show colours. But I need to resolve the circular references like in the other places I used. I cant make it with RxJs SwitchMap operator. Is there any other possible way to resolve?

(<any>window).resolveJsonReferences(this.colorService.searchColors(term))

Typeahead with RxJs SwitchMap

 search = (text$: Observable<string>) =>
    text$
    .debounceTime(200)
    .distinctUntilChanged()
    .do(() => this.searching = true)
    .switchMap(term =>
        this.colorService.searchColors(term) // Json result needs circular reference resolver
        .do(() => this.searchFailed = false)
        .catch(() => {
            this.searchFailed = true;
            return Observable.of([]);
        }))
    .do(() => this.searching = false);

In Other places

return this.colorService.searchColors(term)
        .subscribe(
        res => {               
            this.colors= this.utilities.resolveJsonReferences(res);
        },
        err => {               
        });

Solution

  • Just map the value through your resolver.

    .map((term) => this.utilities.resolveJsonReferences(term))
    

    All together:

    search = (text$: Observable<string>) =>
        text$
        .debounceTime(200)
        .distinctUntilChanged()
        .do(() => this.searching = true)
        .switchMap(term =>
            this.colorService.searchColors(term) // Json result needs circular reference resolver
            .map((term) => this.utilities.resolveJsonReferences(term))
            .do(() => this.searchFailed = false)
            .catch(() => {
                this.searchFailed = true;
                return Observable.of([]);
            }))
        .do(() => this.searching = false);