L.Vardan L.Vardan - 1 year ago 129
Javascript Question

Stop Collection.fetch() in backbone.js

I everyone I am making Web application with backbone.js. I have Collection and I need to stop collection.fetch() before its end. this is my collection.


define(['backbone', 'root/config'], function(Backbone, MainConfig){
var supplyChainCollection = Backbone.Collection.extend({
/**Doing a lot of stuff here can`t show**/
return supplyChainCollection;

Fragment of View

initialize: function(){
this.collection = new SupplyChainCollection();
events :{
'click .close': 'stopFetching'
stopFetching: function(ev){
/*need stop fetch here*/

Thanks in advance.

Answer Source

You need to use .abort() method to stop your request. You can do it in this way. I will not write full code only part that you need to change.

In View

initialize: function(){
    this.collection = new SupplyChainCollection();
    this.fetchXhr = this.collection.fetch();

stopFetching: function(event){
    /** If you need to check readystate**/
    if(this.fetchXhr.readyState > 0 && this.fetchXhr.readyState < 4){
    /**else just use **/

it is not so hard us you think.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download