Commit 8e7aaa7f authored by jaubourg's avatar jaubourg

Merge branch '1.8-jsonp-optimize' of git://github.com/gibson042/jquery into pr749

parents 78583d0c 180a997d
(function( jQuery ) { (function( jQuery ) {
var jsc = jQuery.now(), var oldCallbacks = [],
jsre = /(\=)\?(&|$)|\?\?/i, rquestion = /\?/,
jscallbacks = []; rjsonp = /(=)\?(?=&|$)|\?\?/,
nonce = jQuery.now();
// Default jsonp settings // Default jsonp settings
jQuery.ajaxSetup({ jQuery.ajaxSetup({
jsonp: "callback", jsonp: "callback",
jsonpCallback: function() { jsonpCallback: function() {
var callback = jscallbacks.pop() || ( jQuery.expando + "_" + ( jsc++ ) ); var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
this[ callback ] = true; this[ callback ] = true;
return callback; return callback;
} }
...@@ -17,68 +18,70 @@ jQuery.ajaxSetup({ ...@@ -17,68 +18,70 @@ jQuery.ajaxSetup({
// Detect, normalize options and install callbacks for jsonp requests // Detect, normalize options and install callbacks for jsonp requests
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) { jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
var inspectData = ( typeof s.data === "string" ) && /^application\/x\-www\-form\-urlencoded/.test( s.contentType ); var callbackName, overwritten, responseContainer,
data = s.data,
if ( s.dataTypes[ 0 ] === "jsonp" || url = s.url,
s.jsonp !== false && ( jsre.test( s.url ) || hasCallback = s.jsonp !== false,
inspectData && jsre.test( s.data ) ) ) { replaceInUrl = hasCallback && rjsonp.test( url ),
replaceInData = hasCallback && typeof data === "string" &&
var responseContainer, !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") &&
jsonpCallback = s.jsonpCallback = rjsonp.test( data );
jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
previous = window[ jsonpCallback ], // Handle iff the expected data type is "jsonp" or we have a parameter to set
url = s.url, if ( s.dataTypes[ 0 ] === "jsonp" || replaceInUrl || replaceInData ) {
data = s.data,
replace = "$1" + jsonpCallback + "$2"; // Get callback name, remembering preexisting value associated with it
callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
if ( s.jsonp !== false ) { s.jsonpCallback() :
url = url.replace( jsre, replace ); s.jsonpCallback;
if ( s.url === url ) { overwritten = window[ callbackName ];
if ( inspectData ) {
data = data.replace( jsre, replace ); // Insert callback into url or form data
} if ( replaceInUrl ) {
if ( s.data === data ) { s.url = url.replace( rjsonp, "$1" + callbackName );
// Add callback manually } else if ( replaceInData ) {
url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback; s.data = data.replace( rjsonp, "$1" + callbackName );
} } else if ( hasCallback ) {
} s.url += ( rquestion.test( url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
} }
s.url = url; // Use data converter to retrieve json after script execution
s.data = data; s.converters["script json"] = function() {
if ( !responseContainer ) {
jQuery.error( callbackName + " was not called" );
}
return responseContainer[ 0 ];
};
// force json dataType
s.dataTypes[ 0 ] = "json";
// Install callback // Install callback
window[ jsonpCallback ] = function( response ) { window[ callbackName ] = function() {
responseContainer = [ response ]; responseContainer = arguments;
}; };
// Clean-up function // Clean-up function (fires after converters)
jqXHR.always(function() { jqXHR.always(function() {
// Set callback back to previous value // Restore preexisting value
window[ jsonpCallback ] = previous; window[ callbackName ] = overwritten;
// Save back as free // Save back as free
if ( s[ jsonpCallback ] ) { if ( s[ callbackName ] ) {
// make sure that re-using the options doesn't screw things around // make sure that re-using the options doesn't screw things around
s.jsonpCallback = originalSettings.jsonpCallback; s.jsonpCallback = originalSettings.jsonpCallback;
// save the callback name for future use // save the callback name for future use
jscallbacks.push( jsonpCallback ); oldCallbacks.push( callbackName );
}
// Call if it was a function and we have a response
if ( responseContainer && jQuery.isFunction( previous ) ) {
window[ jsonpCallback ]( responseContainer[ 0 ] );
} }
});
// Use data converter to retrieve json after script execution // Call if it was a function and we have a response
s.converters["script json"] = function() { if ( responseContainer && jQuery.isFunction( overwritten ) ) {
if ( !responseContainer ) { overwritten( responseContainer[ 0 ] );
jQuery.error( jsonpCallback + " was not called" );
} }
return responseContainer[ 0 ];
};
// force json dataType responseContainer = overwritten = undefined;
s.dataTypes[ 0 ] = "json"; });
// Delegate to script // Delegate to script
return "script"; return "script";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment