I use Kendo UI for Angular Grid with data binding. I have two services: one to emit search event from filter component to grid component and one (based on BehaviorSubject) to call API.
The way of data binding with Grid that I use is based on Kendo examples. The problem is how to use switchMap to cancel API requests when user is typing text in filter input. I know the switchMap operator but i don't know how to use it in Kendo UI Grid based Service. I have tried many ways without success and i have no more ideas. My code below:
API Service methods used in Grid component
public query(state: any): void {
this.getItems(state)
.subscribe(x =>
super.next(x));
}
private getItems(state: any): Observable<GridDataResult>{
let page = state.skip / state.take + 1;
let queryStr =
`page=` + page +
`&size=` + state.take +
`&sortColumn=` + state.sortColumn +
`&sortDirection=` + state.sortDirection;
return this.http
.get(`${this.BASE_URL}FindItemsComplex?${queryStr}`)
.pipe(
map(response => (<GridDataResult>{
data: response['data'],
total: response['length']
}))
);
}
Grid Component methods
ngOnInit() {
this.subscription = this.searchService.searchEvent.
debounceTime(400).
subscribe((searchModel: ItemsFilter) =>
{
this.state = searchModel;
this.state.skip = 0;
this.state.take = 15;
this.service.query(this.state);
}
);
}
Where and how i should use switchMap? Thank you for your help.
EDIT
Filter component method used for emitting event
HTML
<input kendoTextBox [(ngModel)]="itemsFilter.textSearch" (ngModelChange)="onSearch()" [ngClass]="'form-control'" />
TS
onSearch() {
this.searchService.Search(this.itemsFilter);
}
Search service
export class ItemsSearchService {
constructor() {
this.searchEvent = new EventEmitter();
}
Search(query :ItemsFilter) {
this.searchEvent.emit(query);
}
searchEvent: EventEmitter<ItemsFilter>;
}