Skip to content
Permalink
Browse files

Merge branch '1.8-jsonp-optimize' of git://github.com/gibson042/jquer…

…y into pr749
  • Loading branch information
jaubourg committed Apr 22, 2012
2 parents 78583d0 + 180a997 commit 8e7aaa7fd0784d1afab2e5fe7a8d347d5f3af4db
Showing with 54 additions and 51 deletions.
  1. +54 −51 src/ajax/jsonp.js
@@ -1,14 +1,15 @@
(function( jQuery ) {

var jsc = jQuery.now(),
jsre = /(\=)\?(&|$)|\?\?/i,
jscallbacks = [];
var oldCallbacks = [],
rquestion = /\?/,
rjsonp = /(=)\?(?=&|$)|\?\?/,
nonce = jQuery.now();

// Default jsonp settings
jQuery.ajaxSetup({
jsonp: "callback",
jsonpCallback: function() {
var callback = jscallbacks.pop() || ( jQuery.expando + "_" + ( jsc++ ) );
var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
this[ callback ] = true;
return callback;
}
@@ -17,68 +18,70 @@ jQuery.ajaxSetup({
// Detect, normalize options and install callbacks for jsonp requests
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {

var inspectData = ( typeof s.data === "string" ) && /^application\/x\-www\-form\-urlencoded/.test( s.contentType );

if ( s.dataTypes[ 0 ] === "jsonp" ||
s.jsonp !== false && ( jsre.test( s.url ) ||
inspectData && jsre.test( s.data ) ) ) {

var responseContainer,
jsonpCallback = s.jsonpCallback =
jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
previous = window[ jsonpCallback ],
url = s.url,
data = s.data,
replace = "$1" + jsonpCallback + "$2";

if ( s.jsonp !== false ) {
url = url.replace( jsre, replace );
if ( s.url === url ) {
if ( inspectData ) {
data = data.replace( jsre, replace );
}
if ( s.data === data ) {
// Add callback manually
url += (/\?/.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
}
}
var callbackName, overwritten, responseContainer,
data = s.data,
url = s.url,
hasCallback = s.jsonp !== false,
replaceInUrl = hasCallback && rjsonp.test( url ),
replaceInData = hasCallback && typeof data === "string" &&
!( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") &&
rjsonp.test( data );

// Handle iff the expected data type is "jsonp" or we have a parameter to set
if ( s.dataTypes[ 0 ] === "jsonp" || replaceInUrl || replaceInData ) {

// Get callback name, remembering preexisting value associated with it
callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
s.jsonpCallback() :
s.jsonpCallback;
overwritten = window[ callbackName ];

// Insert callback into url or form data
if ( replaceInUrl ) {
s.url = url.replace( rjsonp, "$1" + callbackName );
} else if ( replaceInData ) {
s.data = data.replace( rjsonp, "$1" + callbackName );
} else if ( hasCallback ) {
s.url += ( rquestion.test( url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
}

s.url = url;
s.data = data;
// Use data converter to retrieve json after script execution
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
window[ jsonpCallback ] = function( response ) {
responseContainer = [ response ];
window[ callbackName ] = function() {
responseContainer = arguments;
};

// Clean-up function
// Clean-up function (fires after converters)
jqXHR.always(function() {
// Set callback back to previous value
window[ jsonpCallback ] = previous;
// Restore preexisting value
window[ callbackName ] = overwritten;

// Save back as free
if ( s[ jsonpCallback ] ) {
if ( s[ callbackName ] ) {
// make sure that re-using the options doesn't screw things around
s.jsonpCallback = originalSettings.jsonpCallback;

// save the callback name for future use
jscallbacks.push( jsonpCallback );
}
// Call if it was a function and we have a response
if ( responseContainer && jQuery.isFunction( previous ) ) {
window[ jsonpCallback ]( responseContainer[ 0 ] );
oldCallbacks.push( callbackName );
}
});

// Use data converter to retrieve json after script execution
s.converters["script json"] = function() {
if ( !responseContainer ) {
jQuery.error( jsonpCallback + " was not called" );
// Call if it was a function and we have a response
if ( responseContainer && jQuery.isFunction( overwritten ) ) {
overwritten( responseContainer[ 0 ] );
}
return responseContainer[ 0 ];
};

// force json dataType
s.dataTypes[ 0 ] = "json";
responseContainer = overwritten = undefined;
});

// Delegate to script
return "script";

0 comments on commit 8e7aaa7

Please sign in to comment.
You can’t perform that action at this time.