Is it possible to stop Backbone "read" requests

Xerri picture Xerri · Oct 5, 2012 · Viewed 9.7k times · Source

I have a backbone application that has a number of views. Switching between views triggers Ajax requests to get different collections. I would like to stop the current "read" ajax request if a new one is started. Is it possible?

Answer

Xerri picture Xerri · Oct 15, 2012

Ok so here is what I did

I am saving the the fetch requests in a variable

app.fetchXhr = this.model.fetch();

In my router, I have a function that takes care of closing the views and rendering views. It also takes care of triggering any triggers needed for each view change but that is not relevant in this question.

Before doing anything, this router function executes the following

//Stop pending fetch
if(app.fetchXhr.readyState > 0 && app.fetchXhr.readyState < 4){
    app.fetchXhr.abort();
}

I hope this helps