1
YUI.add('jsonp-url', function (Y, NAME) {
3
var JSONPRequest = Y.JSONPRequest,
4
getByPath = Y.Object.getValue,
8
* Adds support for parsing complex callback identifiers from the jsonp url.
9
* This includes callback=foo[1]bar.baz["goo"] as well as referencing methods
10
* in the YUI instance.
13
* @submodule jsonp-url
17
Y.mix(JSONPRequest.prototype, {
19
* RegExp used by the default URL formatter to insert the generated callback
20
* name into the JSONP url. Looks for a query param callback=. If a value
21
* is assigned, it will be clobbered.
25
* @default /\bcallback=.*?(?=&|$)/i
28
_pattern: /\bcallback=(.*?)(?=&|$)/i,
31
* Template used by the default URL formatter to add the callback function
36
* @default "callback={callback}"
39
_template: "callback={callback}",
42
* <p>Parses the url for a callback named explicitly in the string.
43
* Override this if the target JSONP service uses a different query
44
* parameter or url format.</p>
46
* <p>If the callback is declared inline, the corresponding function will
47
* be returned. Otherwise null.</p>
49
* @method _defaultCallback
50
* @param url {String} the url to search in
51
* @return {Function} the callback function if found, or null
54
_defaultCallback: function (url) {
55
var match = url.match(this._pattern),
58
locator, path, callback;
61
// Strip the ["string keys"] and [1] array indexes
63
.replace(/\[(['"])(.*?)\1\]/g,
64
function (x, $1, $2) {
68
.replace(/\[(\d+)\]/g,
70
keys[i] = parseInt($1, 10) | 0;
73
.replace(/^\./, ''); // remove leading dot
75
// Validate against problematic characters.
76
if (!/[^\w\.\$@]/.test(locator)) {
77
path = locator.split('.');
78
for (i = path.length - 1; i >= 0; --i) {
79
if (path[i].charAt(0) === '@') {
80
path[i] = keys[parseInt(path[i].substr(1), 10)];
84
// First look for a global function, then the Y, then try the Y
85
// again from the second token (to support "callback=Y.handler")
86
callback = getByPath(Y.config.win, path) ||
88
getByPath(Y, path.slice(1));
92
return callback || noop;
96
* URL formatter that looks for callback= in the url and appends it
97
* if not present. The supplied proxy name will be assigned to the query
98
* param. Override this method by passing a function as the
99
* "format" property in the config object to the constructor.
102
* @param url { String } the original url
103
* @param proxy {String} the function name that will be used as a proxy to
104
* the configured callback methods.
105
* @return {String} fully qualified JSONP url
108
_format: function (url, proxy) {
109
var callbackRE = /\{callback\}/,
112
if (callbackRE.test(url)) {
113
return url.replace(callbackRE, proxy);
116
callback = this._template.replace(callbackRE, proxy);
118
if (this._pattern.test(url)) {
119
return url.replace(this._pattern, callback);
121
lastChar = url.slice(-1);
122
if (lastChar !== '&' && lastChar !== '?') {
123
url += (url.indexOf('?') > -1) ? '&' : '?';
125
return url + callback;
132
}, '@VERSION@', {"requires": ["jsonp"]});