# Yahoo! UI Library

### connection

Yahoo! UI Library > connection > YAHOO.util.Connect

## Class YAHOO.util.Connect

The Connection Manager provides a simplified interface to the XMLHttpRequest object. It handles cross-browser instantiantion of XMLHttpRequest, negotiates the interactive states and server response, returning the results to a pre-defined callback you create.

### Properties

#### _default_post_header `- private static boolean`

Determines if a default header of Content-Type of 'application/x-www-form-urlencoded' will be added to any client HTTP headers sent for POST transactions.

#### _formNode `- private static object`

Property modified by setForm() to set a reference to the HTML form node if the desired action is file upload.

#### _has_http_headers `- private static boolean`

Determines if HTTP headers are set.

#### _http_header `- private static object`

Object literal of HTTP header(s)

#### _isFileUpload `- private static boolean`

Property modified by setForm() to determine if a file(s) upload is expected.

#### _isFormSubmit `- private static boolean`

Property modified by setForm() to determine if the data should be submitted as an HTML form.

#### _msxml_progid `- private static array`

Array of MSFT ActiveX ids for XMLHttpRequest.

#### _poll `- private static object`

Collection of polling references to the polling mechanism in handleReadyState.

#### _polling_interval `- private static int`

The polling frequency, in milliseconds, for HandleReadyState. when attempting to determine a transaction's XHR readyState. The default is 50 milliseconds.

#### _sFormData `- private static string`

Property modified by setForm() to set the HTML form data for each transaction.

#### _timeOut `- private static object`

Queue of timeout values for each transaction callback with a defined timeout value.

#### _transaction_id `- private static int`

A transaction counter that increments the transaction id for each transaction.

#### _use_default_post_header `- private static boolean`

Determines if a default header of Content-Type of 'application/x-www-form-urlencoded' will be added to any client HTTP headers sent for POST transactions.

### Methods

#### abort

`static boolean abort ( o , callback , isTimeout )`
Method to terminate a transaction, if it has not reached readyState 4.

Parameters:
  `o <object>` The connection object returned by asyncRequest.
  `callback <object>` User-defined callback object.
  `isTimeout <string>` boolean to indicate if abort was a timeout.

Returns: `boolean`

#### appendPostData

`private static array appendPostData ( postData )`
Parses the POST data and creates hidden form elements for each key-value, and appends them to the HTML form object.

Parameters:
  `postData <string>` The HTTP POST data

Returns: `array`
  formElements Collection of hidden fields.

#### asyncRequest

`static object asyncRequest ( method , uri , callback , postData )`
Method for initiating an asynchronous request via the XHR object.

Parameters:
  `method <string>` HTTP transaction method
  `uri <string>` Fully qualified path of resource
  `callback <callback>` User-defined callback function or object
  `postData <string>` POST body

Returns: `object`
  Returns the connection object

#### createExceptionObject

`private static object createExceptionObject ( tId , callbackArg , isAbort )`
If a transaction cannot be completed due to dropped or closed connections, there may be not be enough information to build a full response object. The failure callback will be fired and this specific condition can be identified by a status property value of 0. If an abort was successful, the status property will report a value of -1.

Parameters:
  `tId <int>` The Transaction Id
  `callbackArg <callbackArg>` The user-defined argument or arguments to be passed to the callback
  `isAbort <boolean>` Determines if the exception case is caused by a transaction abort

Returns: `object`

#### createFrame

`private static void createFrame ( secureUri )`
Creates an iframe to be used for form file uploads. It is remove from the document upon completion of the upload transaction.

Parameters:
  `secureUri <string>` Optional qualified path of iframe resource for SSL in IE.

Returns: `void`

#### createResponseObject

`private static object createResponseObject ( o , callbackArg )`
This method evaluates the server response, creates and returns the results via its properties. Success and failure cases will differ in the response object's property values.

Parameters:
  `o <object>` The connection object
  `callbackArg <callbackArg>` The user-defined argument or arguments to be passed to the callback

Returns: `object`

#### createXhrObject

`private static void createXhrObject ( transactionId )`
Instantiates a XMLHttpRequest object and returns an object with two properties: the XMLHttpRequest instance and the transaction id.

Parameters:
  `transactionId <int>` Property containing the transaction id for this transaction.

Returns: `void`
  object

#### getConnectionObject

`private static object getConnectionObject ( )`
This method is called by asyncRequest to create a valid connection object for the transaction. It also passes a transaction id and increments the transaction id counter.

Returns: `object`

#### handleReadyState

`private static void handleReadyState ( o , callback )`
This method serves as a timer that polls the XHR object's readyState property during a transaction, instead of binding a callback to the onreadystatechange event. Upon readyState 4, handleTransactionResponse will process the response, and the timer will be cleared.

Parameters:
  `o <object>` The connection object
  `callback <callback>` The user-defined callback object

Returns: `void`

#### handleTransactionResponse

`private static void handleTransactionResponse ( o , callback , isAbort )`
This method attempts to interpret the server response and determine whether the transaction was successful, or if an error or exception was encountered.

Parameters:
  `o <object>` The connection object
  `callback <object>` The sser-defined callback object
  `isAbort <boolean>` Determines if the transaction was aborted.

Returns: `void`

#### initHeader

`static void initHeader ( label , value )`
Public method that stores the custom HTTP headers for each transaction.

Parameters:
  `label <string>` The HTTP header label
  `value <string>` The HTTP header value

Returns: `void`

#### isCallInProgress

`static boolean isCallInProgress ( o )`
Public method to check if the transaction is still being processed.

Parameters:
  `o <object>` The connection object returned by asyncRequest

Returns: `boolean`

#### releaseObject

`private static void releaseObject ( o )`
Dereference the XHR instance and the connection object after the transaction is completed.

Parameters:
  `o <object>` The connection object

Returns: `void`

#### resetFormState

`private static void resetFormState ( )`
Resets HTML form properties when an HTML form or HTML form with file upload transaction is sent.

Returns: `void`

#### setDefaultPostHeader

`static void setDefaultPostHeader ( b )`
Member to enable or disable the default POST header.

Parameters:
  `b <boolean>` Set and use default header - true or false .

Returns: `void`
  void

#### setForm

`static string setForm ( form , optional )`
This method assembles the form label and value pairs and constructs an encoded string. asyncRequest() will automatically initialize the transaction with a HTTP header Content-Type of application/x-www-form-urlencoded.

Parameters:
  `form <string || object>` id or name attribute, or form object.
  `optional <string>` boolean to indicate SSL environment.
  `optional <string || boolean>` qualified path of iframe resource for SSL in IE.

Returns: `string`
  string of the HTML form field name and value pairs..

#### setHeader

`private static void setHeader ( o )`
Accessor that sets the HTTP headers for each transaction.

Parameters:
  `o <object>` The connection object for the transaction.

Returns: `void`

#### setPollingInterval

`static void setPollingInterval ( i )`
Member to modify the default polling interval.

Parameters:
  `i <int>` The polling interval in milliseconds.

Returns: `void`
  void

#### setProgId

`static void setProgId ( id )`
Member to add an ActiveX id to the existing xml_progid array. In the event(unlikely) a new ActiveX id is introduced, it can be added without internal code modifications.

Parameters:
  `id <string>` The ActiveX id to be added to initialize the XHR object.

Returns: `void`
  void

#### uploadFile

`private static void uploadFile ( id , callback , uri )`
Uploads HTML form, including files/attachments, to the iframe created in createFrame.

Parameters:
  `id <int>` The transaction id.
  `callback <object>` - User-defined callback object.
  `uri <string>` Fully qualified path of resource.

Returns: `void`

Copyright © 2006 Yahoo! Inc. All rights reserved.