xonecas.io
286
Sf

simple-flask-coffee-sass-boilerplate / files

simple-flask-coffee-sass-boilerplate / bower_components / jquery / src / ajax / jsonp.js

2.5 KB
1define([
2	"../core",
3	"./var/nonce",
4	"./var/rquery",
5	"../ajax"
6], function( jQuery, nonce, rquery ) {
7
8var oldCallbacks = [],
9	rjsonp = /(=)\?(?=&|$)|\?\?/;
10
11// Default jsonp settings
12jQuery.ajaxSetup({
13	jsonp: "callback",
14	jsonpCallback: function() {
15		var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
16		this[ callback ] = true;
17		return callback;
18	}
19});
20
21// Detect, normalize options and install callbacks for jsonp requests
22jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
23
24	var callbackName, overwritten, responseContainer,
25		jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
26			"url" :
27			typeof s.data === "string" && !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data"
28		);
29
30	// Handle iff the expected data type is "jsonp" or we have a parameter to set
31	if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) {
32
33		// Get callback name, remembering preexisting value associated with it
34		callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
35			s.jsonpCallback() :
36			s.jsonpCallback;
37
38		// Insert callback into url or form data
39		if ( jsonProp ) {
40			s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
41		} else if ( s.jsonp !== false ) {
42			s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
43		}
44
45		// Use data converter to retrieve json after script execution
46		s.converters["script json"] = function() {
47			if ( !responseContainer ) {
48				jQuery.error( callbackName + " was not called" );
49			}
50			return responseContainer[ 0 ];
51		};
52
53		// force json dataType
54		s.dataTypes[ 0 ] = "json";
55
56		// Install callback
57		overwritten = window[ callbackName ];
58		window[ callbackName ] = function() {
59			responseContainer = arguments;
60		};
61
62		// Clean-up function (fires after converters)
63		jqXHR.always(function() {
64			// Restore preexisting value
65			window[ callbackName ] = overwritten;
66
67			// Save back as free
68			if ( s[ callbackName ] ) {
69				// make sure that re-using the options doesn't screw things around
70				s.jsonpCallback = originalSettings.jsonpCallback;
71
72				// save the callback name for future use
73				oldCallbacks.push( callbackName );
74			}
75
76			// Call if it was a function and we have a response
77			if ( responseContainer && jQuery.isFunction( overwritten ) ) {
78				overwritten( responseContainer[ 0 ] );
79			}
80
81			responseContainer = overwritten = undefined;
82		});
83
84		// Delegate to script
85		return "script";
86	}
87});
88
89});