views:

28

answers:

2

My question is a little bit strange but I "need" to internal purpose update the $.ajax function.

In the original jQuery code, there is:

ajax: function( origSettings ) {
    // Some code
    function success() {
        // If a local callback was specified, fire it and pass it the data
        if ( s.success ) {
            s.success.call( callbackContext, data, status, xhr );
        }

        // Fire the global callback
        if ( s.global ) {
            trigger( "ajaxSuccess", [xhr, s] );
        }
    }
}

And I would like change the internal success function to :

ajax: function( origSettings ) {
    // Some code
    function success() {
        // Fire the global callback
        if ( s.global ) {
            trigger( "ajaxSuccess", [xhr, s] );
        }

        // If a local callback was specified, fire it and pass it the data
        if ( s.success ) {
            s.success.call( callbackContext, data, status, xhr );
        }
    }
}

Trigger changes in this order is more normal and adapted for me...

How can I do this properly ? (If I can...)

Thanks.

A: 

You have to overwrite it, like this.

jQuery.ajax =  
// Some code
function success() {
    // Fire the global callback
    if ( s.global ) {
        trigger( "ajaxSuccess", [xhr, s] );
    }

    // If a local callback was specified, fire it and pass it the data
    if ( s.success ) {
        s.success.call( callbackContext, data, status, xhr );
    }
}

Unfortunately there is no way to overwrite code beyond the Object layer in JS. But consider that this is really not good practice as this will cut you off, of any new bugfixes in JQ.

Can you be more specific what kind of functionality you want to accomplish? Maybe it's simpler to change the other side?

FloydThreepwood
I'm trying to call a generic function on all AJAX queries on the page, and if it fails (for one reason or another) not launch the local callback.
Arnaud F.
A: 

I found a properly way doing this :

$("body").ajaxSend(function(event, XMLHttpRequest, s) {
     s._success = s.success;
     s.success = $.noop;
     s._error = s.error;
     s.error = $.noop;
     s._complete = s.complete;
     s.complete = $.noop;
});

$("body").ajaxSuccess(function(event, XMLHttpRequest, s) {
    // Generic code
    if(s._success) {
        s._success.call(s.context || s, s.data, s.status, XMLHttpRequest)
    }
});

/// ajaxError, ajaxComplete ///
Arnaud F.