I am creating a search page in Angular where the user can search for people via
- the name
- the date of birth
The search method takes the encapsulated search parameters as an argument:
inputName: Subject<string> = new Subject<string>();
inputDate: Subject<Date> = new Subject<Date>();
this.inputName.pipe(distinctUntilChanged(), debounceTime(300)).subscribe(
input => {
this.searchParameters.name = input;
this.search();
}
);
this.inputDate.pipe(distinctUntilChanged(), debounceTime(300)).subscribe(
input => {
this.searchParameters.bornBefore = input;
this.search();
}
);
<input (ngModelChange)="this.inputName.next($event)"
[ngModel]="searchParameters.name"
id="searchName"
name="searchName"
type="text">
<input (ngModelChange)="this.inputDate.next($event)"
[ngModel]="searchParameters.bornBefore"
class="form-control" id="searchDateOfBirth" name="searchDateOfBirth"
type="date">
While this works, I would like to use just one Subject for the query object and debounce on that.
Simply declaring queryParamters: Subject<searchParameters> = new Subject<searchParameters>();
does not work, as changing on filter will clear the other one.
I need to need to allow writing on the searchParamters
object for both input events somehow and combine the changes.