Tobias Bambullis
Tobias Bambullis

Reputation: 249

How to prevent overwriting jQuery's $.ajaxSetup() options?

I've got a question concerning ajaxSetup. When the script starts, I define the following:

$.ajaxSetup({
  success: function(data) {
    example(); 
  }
});

Up to here everything works fine. The problem starts by using Ajaxify, which overwrites the success function.

How can I prevent this? Is there a possibilty to execute both functions or to add the setup success function to that one which I call in the main AJAX request?

Upvotes: 4

Views: 2176

Answers (4)

L Tran
L Tran

Reputation: 11

http://api.jquery.com/Ajax_Events/

You can use global events this way (listed below) instead of using ajaxSetup. This way, the local one would not "override" the global one. They both will be executed.

Global events are triggered on the document, calling any handlers which may be listening. You can listen for these events like so:

// the global event for success is "ajaxSuccess"
$(document).bind("ajaxSuccess", function(){
    // do your function
});

Upvotes: 1

sandex
sandex

Reputation: 51

Use callback function defined in ajaxsetup.
Define

$.ajaxSetup({
    beforeSend      : function() {
        $('#ajaxLoader').show();
        ...
    }
});

and call later

$.ajax({
    beforeSend: function() {
        $.ajaxSettings.beforeSend();
        // do other
        ...
    }
});    

Upvotes: 5

asdf_enel_hak
asdf_enel_hak

Reputation: 7650

in source of jquery-1.4.2.js add "yourFunction()":

function success() {

 yourFunction();    

 // 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] );
 }

}

Edit:

in source of jquery-1.5.1.js add "yourFunction()":

if (isSuccess) {

 yourFunction();

 deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );
} else {
 deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );
}

Upvotes: 3

mVChr
mVChr

Reputation: 50195

Preventing it would involve the ill advised modifying of either jQuery or Ajaxify plugin code since $.ajaxSetup is designed to overwrite old settings. Instead, can't you just use Ajaxify's own onSuccess property? (see: http://max.jsrhost.com/ajaxify-jquery-plugin/)

$('.trigger').ajaxify({
    target: '#element',
    onSuccess: function( options, data ) {
        example();
    }
});

Upvotes: 0

Related Questions