JSONPRequest Class
Provides a JSONPRequest class for repeated JSONP calls, and a convenience method Y.jsonp(url, callback) to instantiate and send a JSONP request.
The callback for the response can be named in the url explicitly or provided in the configuration (second parameter to the constructor).
By default, the query parameter string "callback=???" will be
searched for in the url (??? can be anything). If it's not found, it will
be added on. If the JSONP service uses a different parameter name or url
format, you can override this behavior with the format
property
in the callback config.
The second parameter can be a callback function that accepts the JSON payload as its argument, or a configuration object supporting the keys:
- on - map of callback subscribers
- success - function handler for successful transmission
- failure - function handler for failed transmission
- timeout - function handler for transactions that timeout
- format - override function for inserting the proxy name in the url
- timeout - the number of milliseconds to wait before giving up
- context - becomes
this
in the callbacks - args - array of subsequent parameters to pass to the callbacks
Item Index
Methods
- _format
- _getCallbackFromUrl
- _init
- JSONPRequest._pattern static
- JSONPRequest._template static
- send
- Y.jsonp static
Methods
_format
-
url
-
proxy
Default url formatter. Looks for callback= in the url and appends it if not present. The supplied proxy name will be assigned to the query param. Override this method by passing a function as the "format" property in the config object to the constructor.
Parameters:
-
url
Stringthe original url
-
proxy
Stringthe function name that will be used as a proxy to the configured callback methods.
Returns:
_getCallbackFromUrl
-
url
Parses the url for a callback named explicitly in the string. Override this if the target JSONP service uses a different query parameter or url format.
If the callback is declared inline, the corresponding function will be returned. Otherwise null.
Parameters:
-
url
Stringthe url to search in
Returns:
_init
-
url
-
callback
Set up the success and failure handlers and the regex pattern used to insert the temporary callback name in the url.
Parameters:
-
url
Stringthe url of the JSONP service
-
callback
Object | FunctionOptional success callback or config object containing success and failure functions and the url regex.
JSONPRequest._pattern
()
protected
static
RegExp used by the default URL formatter to insert the generated callback name into the JSONP url. Looks for a query param callback=. If a value is assigned, it will be clobbered.
JSONPRequest._template
()
protected
static
Template used by the default URL formatter to add the callback function name to the url.
send
()
chainable
Issues the JSONP request.
Y.jsonp
-
url
-
c
Parameters:
-
url
Callback{String} the url of the JSONP service with the placeholder where the callback function name typically goes.
-
c
Function | ObjectCallback function accepting the JSON payload as its argument, or a configuration object (see above).