2010-12-09 18:34:28 +00:00
|
|
|
(function( jQuery ) {
|
|
|
|
|
|
|
|
var jsc = jQuery.now(),
|
2011-01-12 17:36:00 +00:00
|
|
|
jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i,
|
2010-12-11 00:35:46 +00:00
|
|
|
rquery_jsonp = /\?/;
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Default jsonp settings
|
|
|
|
jQuery.ajaxSetup({
|
|
|
|
jsonp: "callback",
|
|
|
|
jsonpCallback: function() {
|
|
|
|
return "jsonp" + jsc++;
|
|
|
|
}
|
2010-12-09 18:34:28 +00:00
|
|
|
|
|
|
|
// Normalize jsonp queries
|
|
|
|
// 1) put callback parameter in url or data
|
2011-01-09 20:19:27 +00:00
|
|
|
// 2) sneakily ensure transportDataType is always jsonp for jsonp requests
|
2011-01-13 17:33:24 +00:00
|
|
|
}).ajaxPrefilter("json jsonp", function(s, originalSettings) {
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
if ( s.dataTypes[ 0 ] === "jsonp" ||
|
|
|
|
originalSettings.jsonp ||
|
|
|
|
originalSettings.jsonpCallback ||
|
|
|
|
jsre.test(s.url) ||
|
|
|
|
typeof(s.data) === "string" && jsre.test(s.data) ) {
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
var jsonpCallback = s.jsonpCallback =
|
2010-12-09 18:34:28 +00:00
|
|
|
jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
|
2011-01-12 17:36:00 +00:00
|
|
|
url = s.url.replace(jsre, "$1" + jsonpCallback + "$2"),
|
|
|
|
data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "$1" + jsonpCallback + "$2") : s.data;
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
if ( url === s.url && data === s.data ) {
|
|
|
|
url += (rquery_jsonp.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
|
2010-12-09 18:34:28 +00:00
|
|
|
}
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2010-12-09 18:34:28 +00:00
|
|
|
s.url = url;
|
|
|
|
s.data = data;
|
2011-01-09 20:19:27 +00:00
|
|
|
s.dataTypes[ 0 ] = "jsonp";
|
2010-12-09 18:34:28 +00:00
|
|
|
}
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Bind transport to jsonp dataType
|
2011-01-13 17:33:24 +00:00
|
|
|
}).ajaxTransport("jsonp", function(s) {
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Put callback in place
|
|
|
|
var responseContainer,
|
|
|
|
jsonpCallback = s.jsonpCallback,
|
|
|
|
previous = window[ jsonpCallback ];
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
window [ jsonpCallback ] = function( response ) {
|
|
|
|
responseContainer = [response];
|
|
|
|
};
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
s.complete = [function() {
|
2010-12-09 18:34:28 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Set callback back to previous value
|
|
|
|
window[ jsonpCallback ] = previous;
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Call if it was a function and we have a response
|
|
|
|
if ( previous) {
|
|
|
|
if ( responseContainer && jQuery.isFunction ( previous ) ) {
|
|
|
|
window[ jsonpCallback ] ( responseContainer[0] );
|
2010-12-09 18:34:28 +00:00
|
|
|
}
|
2011-01-09 20:19:27 +00:00
|
|
|
} else {
|
|
|
|
// else, more memory leak avoidance
|
|
|
|
try{ delete window[ jsonpCallback ]; } catch(e){}
|
|
|
|
}
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
}, s.complete ];
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Sneakily ensure this will be handled as json
|
|
|
|
s.dataTypes[ 0 ] = "json";
|
2011-01-05 21:41:23 +00:00
|
|
|
|
2011-01-09 20:19:27 +00:00
|
|
|
// Use data converter to retrieve json after script execution
|
|
|
|
s.converters["script json"] = function() {
|
|
|
|
if ( ! responseContainer ) {
|
|
|
|
jQuery.error( jsonpCallback + " was not called" );
|
|
|
|
}
|
|
|
|
return responseContainer[ 0 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Delegate to script transport
|
|
|
|
return "script";
|
2010-12-09 18:34:28 +00:00
|
|
|
});
|
|
|
|
|
2010-12-10 02:14:03 +00:00
|
|
|
})( jQuery );
|