Created
August 8, 2012 16:55
-
-
Save radusuciu/3296637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (jsonp, d) { | |
console.log('json-p request'); | |
var request = 0, | |
callbacks = {}; | |
// takes callback object of the form: | |
// { | |
// name : 'customCallback', | |
// fun : myCallbackFunction | |
// } | |
// this allows for callbacks with names other than the generic 'callback' or 'jsonp' | |
jsonp.get = function(url, data, callback) { | |
var head = Alt.getTag('head')[0], | |
script = d.createElement('script'); | |
data[callback.name] = 'Alt.jsonp.callbacks.request_' + request; | |
callbacks['request_' + request] = function(data) { | |
head.removeChild(script); | |
delete callbacks['request_' + request]; | |
callback.fun(data); | |
}; | |
request++; | |
if (!Alt.util.isEmpty(data)) url += Alt.util.encodeParams(data); | |
script.type = 'text/javascript'; | |
script.async = true; | |
script.src = url; | |
head.appendChild(script); | |
}; | |
jsonp.callbacks = callbacks; | |
}(Alt.jsonp = Alt.jsonp || {}, document)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment