Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs typeahead: example of local + async data together #3654

Open
robvaneck opened this issue Feb 5, 2018 · 1 comment
Open

docs typeahead: example of local + async data together #3654

robvaneck opened this issue Feb 5, 2018 · 1 comment

Comments

@robvaneck
Copy link

Hi,

[x] doc addition

Could you add an example in the docs how we can use Typahead with locally stored values in (an array) and async data together?

Sometimes request haven't been saved on the server (yet) but have been used locally, so I want to store those locally.. so Typeahead can use them... but merge these local results with the server results. Since RxJS isn't that friendly for beginners, I really need an example.

Thanks

@robvaneck
Copy link
Author

robvaneck commented Feb 5, 2018

I've got this working, and would like to share my code


ngOnInit() {

    this.dataSource = Observable.create((observer: any) => {
      let search: string = this.controlContainer.control.get('name').value;
      let remote = this.someService.search(search);
      
      let query = new RegExp(search, 'ig');
      let locale = Observable.of(this.someService.localValues.filter(x => query.test(x.name)));

      Observable
        .forkJoin([remote, locale], (remote, locale) =>{
          return remote.concat(locale);
        })
        .subscribe(results => {
          observer.next(results);
        });
    });
  }

I also store local values with a subscription on the Typeahead field.. if everything is valid.. i clear the old values from the service (if they match) and push new value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants