www.developer.com/lang/article.php/3600611

Back to Article

Ajax and the Yahoo! Connection Manager
By Nicholas Zakas
April 21, 2006

Advanced Callback Objects

The callback object isn't limited to just two methods, there are a couple other properties provided for ease of use. The first is the argument property, which you can use to pass an argument into the request so it will be returned in the response object. Note that this value is never sent to the server, it is only used for passing data around on the client. You can specify an type of value you want for this property:


var callback = {
    success: function (oResponse) {
        
        //retrieve the argument
        var sArg = oResponse.argument;

    },
    failure: function (oResponse) {

        //retrieve the argument
        var sArg = oResponse.argument;

    },

    argument: "string of info"
}

Once the argument property is defined, it will be returned on the response object that is passed into both the success() and failure() methods. The argument property on the response object contains this value.

There may be a case when you want the success() and failure() methods to call methods on another object. To ease this case, you can use the scope property to set the object scope of methods. For instance, suppose you have an object oObject that has the methods handleSuccess() and handleFailure() that you want to use for success() and failure(), respectively. If you were just to pass in pointers to those methods, you would lose the scope in which they are to be executed (i.e., the this object inside of these methods would not refer to oObject). But, by setting the scope property, you can maintain the proper scope for these methods:

var callback = {
    success: oObject.handleSuccess,
    failure: oObject.handleFailure,
    scope: oObject
}

Monitoring and Managing Requests

One of the limitations of XMLHttp is the lack of a built-in method to monitor and manage multiple requests. The Yahoo! Connection Manager has implemented features that allow you to determine if a request is still pending as well as the ability to abort a request that has not yet completed.

The asyncRequest() method actually returns an object representing the request that was just made. This object can be used later to determine if the request is still pending by passing it to the isCallInProgress() method, like so:

var oConnect = YAHOO.util.Connect.asyncRequest("get", 
	"info.htm", oCallback, null);
alert(YAHOO.util.Connect.isCallInProgress(oConnect)); 
	//outputs "true"

Using this method, you can determine if a particular request has been completed or not. There may even be a time when you have decided that even though the call hasn't completed, you don't want to continue the request. At that time, you can use the abort() method, passing in the same object:

var oConnect = 
YAHOO.util.Connect.asyncRequest("get", 
	"info.htm", oCallback, null);
if(YAHOO.util.Connect.isCallInProgress(oConnect)) {
    YAHOO.util.Connect.abort(oConnect);
}

Calling abort() stops the current request and frees the resources associated with it.

  Go to page: Prev  1  2  3  Next  



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES