Skip to main content

Bug Tracker

Side navigation

#4756 closed feature (worksforme)

Opened June 12, 2009 04:39PM UTC

Closed June 15, 2010 03:18AM UTC

$.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?

Attachments (0)
Change History (3)

Changed June 22, 2009 09:17PM UTC by DarkRyder comment:1

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;
 
	...

Changed June 23, 2009 03:17PM UTC by janosch comment:2

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?

Changed June 15, 2010 03:18AM UTC by dmethvin comment:3

resolution: → worksforme
status: newclosed

See the

context
parameter of the ajax options.