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