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

Feature Proposal: "cancel" callback in fetch options? #83

Open
azaslavsky opened this issue Aug 26, 2016 · 1 comment
Open

Feature Proposal: "cancel" callback in fetch options? #83

azaslavsky opened this issue Aug 26, 2016 · 1 comment

Comments

@azaslavsky
Copy link

azaslavsky commented Aug 26, 2016

I'm running into the following problem: what if, between when I send a fetch call, and when the call returns, something happens in my app that makes me want to cancel the fetch. For example, if a user tries to load more entries in a list, but then, before that XHR response is received, decides to just refresh the list wholesale, I would like to cancel the results of the first call. To be more clear:

1. User presses "see more items" button in list, triggering myCollection.loadNextPageOfResults()
2. User gets impatient, and decides to refresh the whole page
3. For some reason, the API call from #2 returns first.
4. Finally, the API call from #1 returns, but since we have no way to cancel it, it appends the results of myCollection.loadNextPageOfResults() onto the list, even though that is not necessarily desired

What I'd like to implement is a .cancel() option that returns a boolean. If it returns true, then the entire fetch operation is aborted before and set/reset of the actual collection can occur - it will be as though the fetch was never sent to begin with. This could optionally trigger some sort of event as well, if its deemed worthwhile. I think this would be a bit less confusing than the currently undocumented .set property.

Basically, instead of having, the .set conditionals on this line and this line, I would just wrap lines 17-19 in something like if (!opts.cancel || !opts.cancel(self, resp, options)) {}. Thoughts?

@dfdeagle47
Copy link

(Sorry to answer an old question, but it might be useful for future people)

If I'm not mistaken, when you trigger the fetch method on the collection, it returns the XHR object and this object has an abort method to cancel the request, so you could do:

const fetchXHR = collection.fetch();

//...User decides to refesh... abort the request

fetchXHR.abort();

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

No branches or pull requests

2 participants