Skip to content

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

@azaslavsky

Description

@azaslavsky

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions