2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.com/yui/license.html
8
YUI.add('jsonp', function(Y) {
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>
44
* @param url {String} the url of the JSONP service
45
* @param callback {Object|Function} the default callback configuration or
48
function JSONPRequest() {
49
this._init.apply(this, arguments);
52
JSONPRequest.prototype = {
54
* Set up the success and failure handlers and the regex pattern used
55
* to insert the temporary callback name in the url.
58
* @param url {String} the url of the JSONP service
59
* @param callback {Object|Function} Optional success callback or config
60
* object containing success and failure functions and
64
_init : function (url, callback) {
67
// Accept a function, an object, or nothing
68
callback = (isFunction(callback)) ?
69
{ on: { success: callback } } :
72
var subs = callback.on || {};
75
subs.success = this._defaultCallback(url, callback);
78
// Apply defaults and store
79
this._config = Y.merge({
83
}, callback, { on: subs });
87
* Override this method to provide logic to default the success callback if
88
* it is not provided at construction. This is overridden by jsonp-url to
89
* parse the callback from the url string.
91
* @method _defaultCallback
92
* @param url {String} the url passed at construction
93
* @param config {Object} (optional) the config object passed at
97
_defaultCallback: function () {},
100
* Issues the JSONP request.
103
* @param args* {any} any additional arguments to pass to the url formatter
104
* beyond the base url and the proxy function name
108
var args = Y.Array(arguments, 0, true),
110
config = this._config,
113
args.unshift(this.url, 'YUI.Env.JSONP.' + proxy);
114
url = config.format.apply(this, args);
116
if (!config.on.success) {
117
Y.log("No success handler defined. Aborting JSONP request.", "warn", "jsonp");
122
return (isFunction(fn)) ?
124
delete YUI.Env.JSONP[proxy];
125
fn.apply(config.context, [data].concat(config.args));
130
// Temporary un-sandboxed function alias
132
YUI.Env.JSONP[proxy] = wrap(config.on.success);
135
onFailure: wrap(config.on.failure),
136
onTimeout: wrap(config.on.timeout),
137
timeout : config.timeout
144
* Default url formatter. Looks for callback= in the url and appends it
145
* if not present. The supplied proxy name will be assigned to the query
146
* param. Override this method by passing a function as the
147
* "format" property in the config object to the constructor.
150
* @param url { String } the original url
151
* @param proxy {String} the function name that will be used as a proxy to
152
* the configured callback methods.
153
* @param args* {any} additional args passed to send()
154
* @return {String} fully qualified JSONP url
157
_format: function (url, proxy) {
158
return url.replace(/\{callback\}/, proxy);
162
Y.JSONPRequest = JSONPRequest;
167
* @param url {String} the url of the JSONP service with the {callback}
168
* placeholder where the callback function name typically goes.
169
* @param c {Function|Object} Callback function accepting the JSON payload
170
* as its argument, or a configuration object (see above).
171
* @param args* {any} additional arguments to pass to send()
172
* @return {JSONPRequest}
175
Y.jsonp = function (url,c) {
176
var req = new Y.JSONPRequest(url,c);
177
return req.send.apply(req, Y.Array(arguments, 2, true));
180
if (!YUI.Env.JSONP) {
185
}, '3.2.0' ,{requires:['get','oop']});