2

I have created a pagination component including the previous and next buttons on either side of the page numbers.

Every time they press a button on the pagination component (e.g. an available page number, next, or previous), I subscribe to a function in my service that makes a GET request to an API.

This works as expected, however if the user was to spam press any of the buttons, it hits the API multiple times. Considering that subscribing to the HTTP request is an async function, the data that gets returned may not be the latest data the person requested.

Basically if the user hits the button 6 times, but request 5 may have taken longer to retrieve data then request 6, so when it comes to populating my object it gets overwritten with request 5's data, which is not ideal.

I could disable the pagination whilst waiting for a request however this may crop up at another time and I want to be prepared for that inevitability.

What is the best way to handle this?

1
  • you cache the result then : itnext.io/… Commented Mar 20, 2019 at 18:10

2 Answers 2

1

Sounds like you need switchMap() rxjs operator. Since you haven't posted any code, I can give you some pseudocode to get started.

First, set a # reference to the buttons that do next/back navigation in the pagination. As in

<button (click)="doPaginationStuff()" #backButton></button>

Then, declare a @ViewChild() property in your component class. As in

@ViewChild('backButton') buttonRef: ElementRef;

Then, say on ngOnInit() method, you'd subscribe to the click events to the button. As in

ngOnInit() {
  fromEvent(this.buttonRef.nativeElement, 'click')
     .pipe(switchMap(() => this.http.get('whatever page number'))
     .subscribe(console.log));
}

What this does is, it subscribes to button clicks. Once it receives the first button click, it makes the call to the server to get data for the page number passed in the button click. However, if the user keeps pressing the button before the request fulfills, the previous request is cancelled and a new request is made for the page number passed in the latest click. Let me know if this is not what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

0

I think something like this can solve your problem:

  1. assign same class to every paging button. like page-btn

  2. In your component, before you call http method, you can disable every button with above class. (you can do it with angular ElementRefor other class.)

  3. In the subscribe method, you can re enable paging buttons. (This means after complete http request and update ui data)

With this approach, users can not click on any paging button while data request is pending.

2 Comments

Sorry I just updated the message as you posted your answer. I have disabled it for the time being, but I think at some point this may crop up again, is there a way to get the correct data without disabling the buttons?
Depending on how much total data you potentially have you might just page the data into a client side collection asynchronously in the background and page forward and back using the client side collection. This will save you the overhead of making a call each time the user requests another page but could get memory intensive if you are pulling back a lot of data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.