I have a main view that has a box that has subviews that you can switch between
After a fetch ajax call is successful, a 'success' div on the main view is shown.
The problem I'm facing is that, when I switch between subviews, the deferred promise from the previous view's fetch still gets executed and shows the 'success' div.
this is my code:
this.model.fetch().done(function() {
$('#success').show();
});
onChangeSubview: function() {
this.subview.unbind();
this.subview.remove();
}
This SO question abort-ajax-requests-using-jquery says you can store the pointer to the deferred and stop it by calling abort() like so:
var fetchXhr = this.model.fetch().done(function() {
$('#success').show();
});
onChangeSubview: function() {
fetchXhr.abort();
}
I have several fetches for each subview and some are repeated in an interval, that would mean I would have to store every fetch request and loop through them calling abort on each.
Is there a better way to stop the deferred function from being executed? eg: some kind of check to see if the subview has been removed, or a function to unbind all deferred promises?