PositiveGuy
PositiveGuy

Reputation: 47763

Callback to be helper method

I'm trying to refactor a bunch of the same code to just use a helper method as the callback

        $.ajax({
                cache: false,
                type: "POST",
                url: "someUrlHere",
                contentType: "application/json; charset=UTF-8",
                dataType: "json",
                data: JSON.stringify(refundRequest),
                success: onSuccessShowResponseJSON(data, status, jqXHR, refundTransactionResponse)
        });


function onSuccessShowResponseJSON(data, status, jqXHR, showResponseDOMElement)
{
    $('# ' + showResponseDOMElement).show().html(prettifyObject(data), null, '\t');
}

Obivously this does not work, it errors saying it doesn't now what data is. Usually you just state the callback method name (yes I know) but I'm trying to send in an addition item in this case, the refundTransactionResponse that I want to work with also in my helper.

Upvotes: 0

Views: 45

Answers (1)

bipen
bipen

Reputation: 36541

well pass a function inside a success function itself, instead of function reference

 ...
  success: function(data, status, jqXHR){
    onSuccessShowResponseJSON(data, status, jqXHR, refundTransactionResponse);
 }
 ...

Upvotes: 3

Related Questions