3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('jsonp-url', function(Y) {
9 var JSONPRequest = Y.JSONPRequest,
10 getByPath = Y.Object.getValue,
11 noop = function () {};
14 * Adds support for parsing complex callback identifiers from the jsonp url.
15 * This includes callback=foo[1]bar.baz["goo"] as well as referencing methods
16 * in the YUI instance.
19 * @submodule jsonp-url
23 Y.mix(JSONPRequest.prototype, {
25 * RegExp used by the default URL formatter to insert the generated callback
26 * name into the JSONP url. Looks for a query param callback=. If a value
27 * is assigned, it will be clobbered.
31 * @default /\bcallback=.*?(?=&|$)/i
34 _pattern: /\bcallback=(.*?)(?=&|$)/i,
37 * Template used by the default URL formatter to add the callback function
42 * @default "callback={callback}"
45 _template: "callback={callback}",
48 * <p>Parses the url for a callback named explicitly in the string.
49 * Override this if the target JSONP service uses a different query
50 * parameter or url format.</p>
52 * <p>If the callback is declared inline, the corresponding function will
53 * be returned. Otherwise null.</p>
55 * @method _defaultCallback
56 * @param url {String} the url to search in
57 * @return {Function} the callback function if found, or null
60 _defaultCallback: function (url) {
61 var match = url.match(this._pattern),
64 locator, path, callback;
67 // Strip the ["string keys"] and [1] array indexes
69 .replace(/\[(['"])(.*?)\1\]/g,
70 function (x, $1, $2) {
74 .replace(/\[(\d+)\]/g,
76 keys[i] = parseInt($1, 10) | 0;
79 .replace(/^\./, ''); // remove leading dot
81 // Validate against problematic characters.
82 if (!/[^\w\.\$@]/.test(locator)) {
83 path = locator.split('.');
84 for (i = path.length - 1; i >= 0; --i) {
85 if (path[i].charAt(0) === '@') {
86 path[i] = keys[parseInt(path[i].substr(1), 10)];
90 // First look for a global function, then the Y, then try the Y
91 // again from the second token (to support "callback=Y.handler")
92 callback = getByPath(Y.config.win, path) ||
94 getByPath(Y, path.slice(1));
98 return callback || noop;
102 * URL formatter that looks for callback= in the url and appends it
103 * if not present. The supplied proxy name will be assigned to the query
104 * param. Override this method by passing a function as the
105 * "format" property in the config object to the constructor.
108 * @param url { String } the original url
109 * @param proxy {String} the function name that will be used as a proxy to
110 * the configured callback methods.
111 * @return {String} fully qualified JSONP url
114 _format: function (url, proxy) {
115 var callback = this._template.replace(/\{callback\}/, 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 }, '3.5.1' ,{requires:['jsonp']});