Bug Tracker

Opened 10 years ago

Closed 9 years ago

#4756 closed feature (worksforme)

$.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:
Blocked by: Blocking:

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 (3)

comment:1 Changed 10 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 10 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 9 years ago by dmethvin

Resolution: worksforme
Status: newclosed

See the context parameter of the ajax options.

Note: See TracTickets for help on using tickets.