# Yahoo! UI Library

### jsonp 3.3.0

Yahoo! UI Library > jsonp > JSONPRequest
Search:
Filters Show Private Show Protected Show Deprecated

## Class JSONPRequest

Provides a JSONPRequest class for repeated JSONP calls, and a convenience method Y.jsonp(url, callback) to instantiate and send a JSONP request.

Both the constructor as well as the convenience function take two parameters: a url string and a callback.

The url provided must include the placeholder string "{callback}" which will be replaced by a dynamically generated routing function to pass the data to your callback function. An example url might look like "http://example.com/service?callback={callback}".

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
- allowCache - use the same proxy name for all requests? (boolean)

### Constructor

JSONPRequest `( url , callback )`

Parameters:
  `url <String>` the url of the JSONP service
  `callback <Object|Function>` the default callback configuration or success handler

### Properties

#### _pattern - `protected RegExp`

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.
Default Value: /\bcallback=.*?(?=&|$)/i

#### _requests - `private {Number}`

Number of requests currently pending responses. Used by connections configured to allowCache to make sure the proxy isn't deleted until the last response has returned.

#### _template - `protected String`

Template used by the default URL formatter to add the callback function name to the url.
Default Value: "callback={callback}"

### Methods

#### _defaultCallback

`protected Function _defaultCallback ( 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 <String>` the url to search in

Returns: `Function`
  the callback function if found, or null

#### _format

`protected String _format ( url , proxy )`
URL formatter that 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 < String >` the original url
  `proxy <String>` the function name that will be used as a proxy to the configured callback methods.

Returns: `String`
  fully qualified JSONP url

#### _init

`protected void _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 <String>` the url of the JSONP service
  `callback <Object|Function>` Optional success callback or config object containing success and failure functions and the url regex.

#### send

`void send ( args* )`
Issues the JSONP request.

Parameters:
  `args* <any>` any additional arguments to pass to the url formatter beyond the base url and the proxy function name

Chainable: This method is chainable.

#### Y.jsonp

`static JSONPRequest Y.jsonp ( url , c , args* )`

Parameters:
  `url <String} the url of the JSONP service with the {callback>` placeholder where the callback function name typically goes.
  `c <Function|Object>` Callback function accepting the JSON payload as its argument, or a configuration object (see above).
  `args* <any>` additional arguments to pass to send()

Copyright © 2011 Yahoo! Inc. All rights reserved.