2

I am trying to write a JavaScript Object which has many Properties and Methods. The basic function of this code is to send an ajax call and get data from server. Code IS:

function restClient(options) {
    var _response;
    var _response_status;
    var _response_message;
    var _response_data;

    // Default Options
    var _default = {
        restCall: true,
        type: "GET",
        url: '',
        contentType: "application/json; charset=utf-8",
        crossDomain: false,
        cache: false,
        dataType: 'json',
        data: {},
        beforeSend: _ajaxBeforeSend,
        success: _ajaxSuccess,
        error: _ajaxError,
        complete: _ajaxComplete
    };

    // Extend default Options by User Options
    var ajaxOptions = $.extend(_default, options);

    // Private Methods
    function _ajaxBeforeSend() {

    }
    function _ajaxSuccess(response) {
        _response = response;
        _response_status = response.status;
        _response_message = response.message;
        _response_data = response.data;
    }
    function _ajaxError(xhr, status, error) {
        _response_status = xhr.status;
        _response_message = error;
    }

    function _ajaxComplete(xhr, status) {

    }

    // Send Ajax Request
    this.sendRequest = function() {
        $.ajax(ajaxOptions);
    };


    // Get Server Response Pack [status,message,data]
    this.getResponse = function() {
        return _response;
    };

    // Get Server Response Status: 200, 400, 401 etc
    this.getStatus = function() {
        return _response_status;
    };

    // Get Server Message
    this.getMessage = function() {
        return _response_message;
    };

    // Get Server Return Data
    this.getData = function() {
        return _response_data;
    };
}

Now I am trying to create object using new operator and call sendRequest(); method to send an ajax call and then I am calling getResponse(); to get server response like:

var REST = new restClient(options);
REST.sendRequest();
console.log(REST.getResponse());

Every thing is working properly But the problem is REST.getResponse(); call before to complete Ajax which give me empty result. If i do like this

$(document).ajaxComplete(function(){
        console.log(REST.getResponse());
    });

then it work But Still two problems are

  1. If there are another ajax call its also wait for that
  2. its looking bad I want to hide this ajaxComplete() some where within restClient(); Please Help me.

Thanks.

1

1 Answer 1

3

You have to change method sendRequest to accept a callback, that you'll call on response completion.

this.sendRequest = function(cb) {
    this.cb = cb; 
    $.ajax(ajaxOptions);
};

this._ajaxComplete = function(xhr, status) {
    this.cb && this.cb();
}

Also, after defining this._ajaxComplete change the _default.complete handler, in order to bind the this object, otherwise you'll miss the cb property:

_default.complete = this._ajaxComplete.bind(this);

Your client code will become:

var REST = new restClient(options);
REST.sendRequest(function(){
    console.log(REST.getResponse());
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a Lot works with a minor change _ajaxComplete.bind(this); instead of this._ajaxComplete.bind(this); and I am still thinking on this logic :)
It's the same, if you declare this._ajaxComplete = function()... then you'll use this._ajaxComplete.bind(this). If you declare function _ajaxComplete() ... then use _ajaxComplete.bind(this). The bind call return a function "binded" to the argument, it's this variable always refer to the argument you bind it. See MDN bind page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.