-
-
Save brettz9/4303894 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
// Adds one JSONP global (for requireJS module, see JSONP.js) | |
var JSONP = (function (global) { | |
// (C) WebReflection Essential - Mit Style | |
// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL) | |
'use strict'; | |
var id = 0, | |
ns = 'JSONP', | |
prefix = '__JSONP__', | |
document = global.document, | |
documentElement = document.documentElement; | |
return function (uri, callback) { | |
var src = prefix + id++, | |
script = document.createElement('script'), | |
JSONPResponse = function () { | |
try { delete global[ns][src]; } catch(e) { global[ns][src] = null; } | |
documentElement.removeChild(script); | |
callback.apply(this, arguments); | |
}; | |
global[ns][src] = JSONPResponse; | |
documentElement.insertBefore( | |
script, | |
documentElement.lastChild | |
).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src; | |
}; | |
}(this)); |
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
/*globals define */ | |
define(function () { | |
// (C) WebReflection Essential - Mit Style | |
// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL) | |
'use strict'; | |
var id = 0, | |
global = window, | |
ns = 'require', | |
prefix = '__JSONP__', | |
document = global.document, | |
documentElement = document.documentElement; | |
return function (uri, callback) { | |
var src = prefix + id++, | |
script = document.createElement('script'), | |
JSONPResponse = function () { | |
try { delete global[ns][src]; } catch(e) { global[ns][src] = null; } | |
documentElement.removeChild(script); | |
callback.apply(this, arguments); | |
}; | |
global[ns][src] = JSONPResponse; | |
documentElement.insertBefore( | |
script, | |
documentElement.lastChild | |
).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src; | |
}; | |
}); |
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
/*globals JSONP */ | |
(function () {'use strict'; | |
function MediaWikiJSONP(opts, argObj, cb) { | |
if (!(this instanceof MediaWikiJSONP)) { | |
return new MediaWikiJSONP(opts, argObj, cb); | |
} | |
if (typeof opts === 'string') { | |
this.baseURL = opts; | |
} | |
else { | |
this.baseURL = opts.baseURL; | |
} | |
if (argObj) { | |
this.send(argObj, cb); | |
} | |
} | |
MediaWikiJSONP.prototype.send = function MediaWikiJSONP__send (argObj, cb) { | |
cb = cb || function () {}; // Are there API calls with side effects? | |
var uri, arg, args = ''; | |
for (arg in argObj) { | |
if (argObj.hasOwnProperty(arg)) { | |
args += '&' + arg + '=' + encodeURIComponent(argObj[arg]); | |
} | |
} | |
uri = this.baseURL + '/w/api.php?format=json' + args; | |
JSONP(uri, cb); | |
}; | |
// EXPORTS | |
this.MediaWikiJSONP = MediaWikiJSONP; | |
}()); |
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
/*globals define */ | |
define(['./JSONP'], function (JSONP) { | |
'use strict'; | |
function MediaWikiJSONP(opts, argObj, cb) { | |
if (!(this instanceof MediaWikiJSONP)) { | |
return new MediaWikiJSONP(opts, argObj, cb); | |
} | |
if (typeof opts === 'string') { | |
this.baseURL = opts; | |
} | |
else { | |
this.baseURL = opts.baseURL; | |
} | |
if (argObj) { | |
this.send(argObj, cb); | |
} | |
} | |
MediaWikiJSONP.prototype.send = function MediaWikiJSONP__send (argObj, cb) { | |
cb = cb || function () {}; // Are there API calls with side effects? | |
var uri, arg, args = ''; | |
for (arg in argObj) { | |
if (argObj.hasOwnProperty(arg)) { | |
args += '&' + arg + '=' + encodeURIComponent(argObj[arg]); | |
} | |
} | |
uri = this.baseURL + '/w/api.php?format=json' + args; | |
JSONP(uri, cb); | |
}; | |
return MediaWikiJSONP; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment