2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('jsonp', function (Y, NAME) {
10 var isFunction = Y.Lang.isFunction;
13 * <p>Provides a JSONPRequest class for repeated JSONP calls, and a convenience
14 * method Y.jsonp(url, callback) to instantiate and send a JSONP request.</p>
16 * <p>Both the constructor as well as the convenience function take two
17 * parameters: a url string and a callback.</p>
19 * <p>The url provided must include the placeholder string
20 * "{callback}" which will be replaced by a dynamically
21 * generated routing function to pass the data to your callback function.
22 * An example url might look like
23 * "http://example.com/service?callback={callback}".</p>
25 * <p>The second parameter can be a callback function that accepts the JSON
26 * payload as its argument, or a configuration object supporting the keys:</p>
28 * <li>on - map of callback subscribers
30 * <li>success - function handler for successful transmission</li>
31 * <li>failure - function handler for failed transmission</li>
32 * <li>timeout - function handler for transactions that timeout</li>
35 * <li>format - override function for inserting the proxy name in the url</li>
36 * <li>timeout - the number of milliseconds to wait before giving up</li>
37 * <li>context - becomes <code>this</code> in the callbacks</li>
38 * <li>args - array of subsequent parameters to pass to the callbacks</li>
39 * <li>allowCache - use the same proxy name for all requests? (boolean)</li>
45 * @param url {String} the url of the JSONP service
46 * @param callback {Object|Function} the default callback configuration or
49 function JSONPRequest() {
50 this._init.apply(this, arguments);
53 JSONPRequest.prototype = {
55 * Set up the success and failure handlers and the regex pattern used
56 * to insert the temporary callback name in the url.
59 * @param url {String} the url of the JSONP service
60 * @param callback {Object|Function} Optional success callback or config
61 * object containing success and failure functions and
65 _init : function (url, callback) {
69 * Map of the number of requests currently pending responses per
70 * generated proxy. Used to ensure the proxy is not flushed if the
71 * request times out and there is a timeout handler and success
72 * handler, and used by connections configured to allowCache to make
73 * sure the proxy isn't deleted until the last response has returned.
82 * Map of the number of timeouts received from the destination url
83 * by generated proxy. Used to ensure the proxy is not flushed if the
84 * request times out and there is a timeout handler and success
85 * handler, and used by connections configured to allowCache to make
86 * sure the proxy isn't deleted until the last response has returned.
94 // Accept a function, an object, or nothing
95 callback = (isFunction(callback)) ?
96 { on: { success: callback } } :
99 var subs = callback.on || {};
102 subs.success = this._defaultCallback(url, callback);
105 // Apply defaults and store
106 this._config = Y.merge({
109 format : this._format,
111 }, callback, { on: subs });
115 * Override this method to provide logic to default the success callback if
116 * it is not provided at construction. This is overridden by jsonp-url to
117 * parse the callback from the url string.
119 * @method _defaultCallback
120 * @param url {String} the url passed at construction
121 * @param config {Object} (optional) the config object passed at
125 _defaultCallback: function () {},
128 * Issues the JSONP request.
131 * @param args* {any} any additional arguments to pass to the url formatter
132 * beyond the base url and the proxy function name
137 args = Y.Array(arguments, 0, true),
138 config = self._config,
139 proxy = self._proxy || Y.guid(),
142 // TODO: support allowCache as time value
143 if (config.allowCache) {
147 if (self._requests[proxy] === undefined) {
148 self._requests[proxy] = 0;
150 if (self._timeouts[proxy] === undefined) {
151 self._timeouts[proxy] = 0;
153 self._requests[proxy]++;
155 Y.log('sending ' + proxy);
157 args.unshift(self.url, 'YUI.Env.JSONP.' + proxy);
158 url = config.format.apply(self, args);
160 if (!config.on.success) {
161 Y.log("No success handler defined. Aborting JSONP request.", "warn", "jsonp");
165 function wrap(fn, isTimeout) {
166 return (isFunction(fn)) ?
169 counter = '_requests';
171 //if (config.allowCache) {
172 // A lot of wrangling to make sure timeouts result in
173 // fewer success callbacks, but the proxy is properly
176 ++self._timeouts[proxy];
177 --self._requests[proxy];
178 Y.log(proxy + ' timed out - timeouts(' + self._timeouts[proxy] + ') requests(' + self._requests[proxy] + ')');
180 if (!self._requests[proxy]) {
182 counter = '_timeouts';
184 --self[counter][proxy];
185 Y.log(proxy + ' response received - timeouts(' + self._timeouts[proxy] + ') requests(' + self._requests[proxy] + ')');
189 if (!self._requests[proxy] && !self._timeouts[proxy]) {
190 Y.log('deleting ' + proxy);
191 delete YUI.Env.JSONP[proxy];
195 fn.apply(config.context, [data].concat(config.args));
201 // Temporary un-sandboxed function alias
203 YUI.Env.JSONP[proxy] = wrap(config.on.success);
205 // Y.Get transactions block each other by design, but can easily
206 // be made non-blocking by just calling execute() on the transaction.
207 // https://github.com/yui/yui3/pull/393#issuecomment-11961608
209 onFailure : wrap(config.on.failure),
210 onTimeout : wrap(config.on.timeout, true),
211 timeout : config.timeout,
212 charset : config.charset,
213 attributes: config.attributes,
221 * Default url formatter. Looks for callback= in the url and appends it
222 * if not present. The supplied proxy name will be assigned to the query
223 * param. Override this method by passing a function as the
224 * "format" property in the config object to the constructor.
227 * @param url { String } the original url
228 * @param proxy {String} the function name that will be used as a proxy to
229 * the configured callback methods.
230 * @param args* {any} additional args passed to send()
231 * @return {String} fully qualified JSONP url
234 _format: function (url, proxy) {
235 return url.replace(/\{callback\}/, proxy);
239 Y.JSONPRequest = JSONPRequest;
244 * @param url {String} the url of the JSONP service with the {callback}
245 * placeholder where the callback function name typically goes.
246 * @param c {Function|Object} Callback function accepting the JSON payload
247 * as its argument, or a configuration object (see above).
248 * @param args* {any} additional arguments to pass to send()
249 * @return {JSONPRequest}
253 Y.jsonp = function (url,c) {
254 var req = new Y.JSONPRequest(url,c);
255 return req.send.apply(req, Y.Array(arguments, 2, true));
258 if (!YUI.Env.JSONP) {
263 }, '3.13.0', {"requires": ["get", "oop"]});