Search code examples
javascriptjqueryknockout.jsabort

knockout : Ajax request abort


I want to prevent previous ajax request when new request is running. I do this like below code. But, it's not working.

I added code in beforeSend() function for abort previous request. But, that's not working.

Please help me to solve this.

jQuery :

return Component.extend({
    defaults: {
        changeTextValue: ko.observable(),
        currentRequest: ko.observable(),
        anotherObservableArray: [],
        subscription: null,
        tracks: {
            changeTextValue: true
        }
    },
    initialize: function() {
        this._super();
    },
    doSomething: function(config) {
        var self = this;
        if (this.subscription)
            this.subscription.dispose();

        this.subscription = this.changeTextValue.subscribe(function(newValue) {
            this.currentRequest = $.ajax({
                url: urlBuilder.build('abc/temp/temp'),
                type: 'POST',
                data: JSON.stringify({
                    'searchtext': newValue
                }),
                global: true,
                contentType: 'application/json',
                beforeSend: function(jqXHR) {
                    console.log("before Ajax send");
                    console.log(this.currentRequest);
                    if (this.currentRequest != null) {
                        this.currentRequest.abort();
                    }
                },
                success: function(response) {
                    var json_data = JSON.parse(response);
                    self.autocompleteData.removeAll();
                    $.each(json_data, function(key, val) {
                        self.autocompleteData.push({
                            productID: val.productID
                        });
                    });
                },
                error: function(jqXHR, exception) {
                    alert("Not OK!")
                }
            });
        });
    },
});
ko.applyBindings(new CeremonyViewModel());

Solution

  • So I'm guessing you want to cancel the previous request when the subscribe function is called again, correct?

    In that case, try aborting it if the subscribe is called again.

    return Component.extend({
        defaults: {
            changeTextValue: ko.observable(),
            currentRequest: null,
            anotherObservableArray: [],
            subscription: null,
            tracks: {
                changeTextValue: true
            }
        },
        initialize: function() {
            this._super();
        },
        doSomething: function(config) {
            var self = this;
            if (this.subscription)
                this.subscription.dispose();
    
            this.subscription = this.changeTextValue.subscribe(function(newValue) {
                if (self.currentRequest) {
                    self.currentRequest.abort();
                }
                self.currentRequest = $.ajax({
                    url: urlBuilder.build('abc/temp/temp'),
                    type: 'POST',
                    data: JSON.stringify({
                        'searchtext': newValue
                    }),
                    global: true,
                    contentType: 'application/json',
                    success: function(response) {
                        var json_data = JSON.parse(response);
                        self.autocompleteData.removeAll();
                        $.each(json_data, function(key, val) {
                            self.autocompleteData.push({
                                productID: val.productID
                            });
                        });
                    },
                    error: function(jqXHR, exception) {
                        alert("Not OK!")
                    }
                });
            });
        },
    });
    ko.applyBindings(new CeremonyViewModel());