Bug Tracker

Modify

Ticket #4756 (closed feature: worksforme)

Opened 4 years ago

Last modified 3 years ago

$.ajax() needs a field for opaque data

Reported by: janosch Owned by:
Priority: major Milestone: 1.4
Component: ajax Version: 1.3.2
Keywords: Cc:
Blocking: Blocked by:

Description

It would be extremely useful when one could attach a context pointer to a $.ajax() request that would be returned to the callback somehow.

I first thought this could be solved simply by passing a custom option to the $.ajax() function, like so:

jQuery.ajax({

type: "GET", url: "/x.pl", success: succ_callack, dataType: "json", opaque: this,

});

And in the callback:

function succ_callback(data, status) {

var ctx = this.opaque; ... call ctx.something() or so

}

While this works for simple cases, it leads to an infinite loop if the object passed as "opaque" contains a reference to itself (or another object that contains a reference to "opaque" again). This is caused by the recursive call for $.extend() in jquery-1.3.2.js:597 which is started by $.ajax() in :3400.

Any suggestions how to solve this?

Change History

comment:1 Changed 4 years ago by DarkRyder

Something like the following should work, I believe.

ajax: function( s ) {
	// Guard self-referential opaque arguments against the
	// deep .extend() below to prevent infinite recursion.
	if (typeof s.opaque !== "undefined") {
		var opaque = s.opaque;
		s.opaque = false;
	}
 
	// Extend the settings, but re-extend 's' so that it can be
	// checked again later (in the test suite, specifically)
	s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
 
	if (typeof opaque !== "undefined") s.opaque = opaque;
 
	...

comment:2 Changed 4 years ago by janosch

That's actually what I thought about aswell, but I'm not familiar with jQuery's internals (e.g. if the jQuery.extend must be a deep extend).

If that proposed patch is the best solution, how about integrating this into the next release and documenting the "opaque" context feature? Is there anything else required for this to be accepted?

comment:3 Changed 3 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to worksforme

See the context parameter of the ajax options.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.