jQuery: The Write Less, Do More JavaScript Library

Ticket #1362 (closed feature: fixed)

Opened 1 year ago

Last modified 3 months ago

$.ajax(): pass 'settings' object to 'success' callback

Reported by: Chainfire Assigned to: anonymous
Type: feature Priority: major
Milestone: 1.2.4 Component: ajax
Version: 1.1.3 Keywords:
Cc: Needs: Patch

Description

I'm finding I want to pass data to the complete callback quite often, and I've seen the question come over IRC a couple of times as well. Currently I have this solved by using $().ajaxSuccess, but using the success callback would be neater.

'Current way' of doing it:

$.ajax({ ..., mydata: data});
$(document).ajaxSuccess(function(event, xhr, settings) {
  if (settings.url == ...) { // check for right ajax request
    alert(settings.mydata); // do something useful instead
  }
});

It would be much better if the settings structure was passed to the complete callback as well:

$.ajax({ ..., mydata: data, success: function(data, settings) {
  alert(settings.mydata);
}});

Possibly pass the XHR object as well to make the functions more consistent with the $.ajaxXXXX callbacks. Ofcourse this would apply to the 'complete' callback in much the same way.

Attachments

Change History

Changed 1 year ago by Chainfire

Seems I've been dumb. You can just use 'this' to access the parameter object passed to $.ajax from the callback. That really should be in the $.ajax() docs though.

Changed 3 months ago by flesler

  • status changed from new to closed
  • resolution set to fixed
  • milestone changed from 1.1.4 to 1.2.4

The settings hash is now received by the handler, and (at least now), the docs mention the this.

Note: See TracTickets for help on using tickets.