Yahoo! Developer Network Home - Help

YDN Site Search

# YUI Library Examples: Connection Manager: Subscribing to Connection Manager Global Events

# Connection Manager: Subscribing to Connection Manager Global Events

Connection Manager exposes Custom Events that track the progress of a transaction through its lifecycle. These Custom Events are raised at the global level and at the transaction level. The following code example provides a step-by-step approach to subscribing to global custom events raised by Connection Manager. In this scenario, an event handler object is created to handle all Custom Events. Each Custom Event is explicitly subscribed with a reference to it's event handler.

Click "Send a Request" below to try it out, then read the description below to learn how to subscribe to global Custom Events in Connection Manager.

View example in new window.

## Subscribing to Connection Manager Global Events

### Source file and dependencies

Load the Yahoo Global Object and Connection Manager source file:

<script src="yahoo.js"></script> <script src="event.js"></script> <script src="connection.js"></script>

### Assemble the Querystring

Construct a simple querystring with a key-value pair of `s = hello world`. This data will be sent back to the client as a response from PHP to confirm the transaction.

/* * Create a querystring with example key-value pair of * hellow = world. Remember to encode the querystring * if and when the string contains special characters. */ var sUrl = "php/get.php?s=hello%20world";

### Create an Event Handler Object

Create an object to handle the global custom events fired by Connection Manager.

var div = document.getElementById('container'); var globalEvents = { start:function(type, args){ div.innerHTML += "<li>Transaction " + args[0].tId + " " + type + " fired.<li>"; }, complete:function(type, args){ div.innerHTML += "<li>Transaction " + args[0].tId + " " + type + " fired.<li>"; }, success:function(type, args){ div.innerHTML += "<li>Transaction " + args[0].tId + " " + type + " fired.<li>"; if(args[0].responseText !== undefined){ div.innerHTML += "<li>Transaction id: " + args[0].tId + "<li>"; div.innerHTML += "<li>HTTP status: " + args[0].status + "<li>"; div.innerHTML += "<li>Status code message: " + args[0].statusText + "<li>"; div.innerHTML += "<li>HTTP headers: " + args[0].getAllResponseHeaders + "<li>"; div.innerHTML += "<li>Server response: " + args[0].responseText + "<li>"; div.innerHTML += "<li>Argument object: Array ( [foo] => " + args[0].argument[0] +" [bar] => " + args[0].argument[1] +" )<li>"; } }, failure:function(type, args){ div.innerHTML += "<li>Transaction " + args[0].tId + " " + type + " fired.<li>"; if(args[0].responseText !== undefined){ div.innerHTML += "<li>Transaction id: " + args[0].tId + "<li>"; div.innerHTML += "<li>HTTP status: " + args[0].status + "<li>"; div.innerHTML += "<li>Status code message: " + args[0].statusText + "<li>"; } }, abort:function(type, args){ div.innerHTML += "<li>Transaction " + args[0].tId + " " + type + " fired.<li>"; } };

Let's create two functions to provide handlers for `callback.success` and `callback.failure` compatibility. The inclusion of these handlers will demonstrate the callback object's compatibility with Custom Events.

var handleSuccess = function(o){ div.innerHTML += "<li>Success response handler triggered in callback.success<li>"; }; var handleFailure = function(o){ div.innerHTML += "<li>Failure response handler triggered in callback.success<li>"; };

Subscribe to the global custom events fired by Connection Manager before starting the transaction.

/* Subscribe to global listeners */ // Create a shorthand variable for YAHOO.util.Connect var YCM = YAHOO.util.Connect; YCM.startEvent.subscribe(globalEvents.start); YCM.completeEvent.subscribe(globalEvents.complete); YCM.successEvent.subscribe(globalEvents.success); YCM.failureEvent.subscribe(globalEvents.failure); YCM.abortEvent.subscribe(globalEvents.abort);

### Initiate the Transaction

Call `YAHOO.util.Connect.asyncRequest` to make a request to `get.php`, and PHP will return the contents of `$_GET` via `print_r()`. Each event handler in `globalEvents` will be triggered in response to its corresponding custom event(e.g., globalEvents.start will be called when startEvent fires). In this example, event handlers are created and subscribed to all possible events raised by Connection Manager.

var request = YAHOO.util.Connect.asyncRequest('GET', sUrl);

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Connection Manager Examples:

- Connection Manager assisted image crop (included with examples for ImageCropper Control (beta))
- Connection Manager GET Transaction
- Connection Manager POST Transaction
- Connection Manager Transaction Timeout
- Retrieving a Yahoo! Weather RSS Feed
- Subscribing to Connection Manager Global Events
- Subscribing to Connection Manager's Custom Events via the Callback Object
- Dynamically Loading Node Data (included with examples for TreeView Control)
- Editor Data Post with Connection Manager (included with examples for Rich Text Editor (beta))
- Simple Application (included with examples for Layout Manager (beta))

### More Connection Manager Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings