aboutsummaryrefslogtreecommitdiffstats
path: root/src/ajax
diff options
context:
space:
mode:
authorRichard Gibson <richard.gibson@gmail.com>2012-04-22 11:36:44 -0400
committerRichard Gibson <richard.gibson@gmail.com>2012-04-22 11:36:44 -0400
commitb0ea80e9fd35286c1cd68216c512712dff1f145d (patch)
tree7f96d23e0810b3b83fcf36a64d6659677e7bb9be /src/ajax
parentd3b61de52084353646e1abee7dc7a76c63237b9b (diff)
downloadjquery-b0ea80e9fd35286c1cd68216c512712dff1f145d.tar.gz
jquery-b0ea80e9fd35286c1cd68216c512712dff1f145d.zip
refactor jsonp.js for size and clarity
Diffstat (limited to 'src/ajax')
-rw-r--r--src/ajax/jsonp.js106
1 files changed, 55 insertions, 51 deletions
diff --git a/src/ajax/jsonp.js b/src/ajax/jsonp.js
index 9965b2230..4340d9acc 100644
--- a/src/ajax/jsonp.js
+++ b/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,71 @@ 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" || hasCallback &&
+ ( 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";