More YAHOO.util.Connect.asyncRequest

I recently wrote about YAHOO.util.Connect.asyncRequest from Yahoo and how I used their code for some Ajax. My one problem with my solution was that my callback object was “outside” of all my other code. Today, I came up with such a simple solution. Just delete the callback object and in place of where the connection request asks for a callback object, just use the “this” pointer. Here’s my updated code.

var sentItems =
{
    success: function(o)
    {
        var sent =
            document.getElementById
            ('items_sent_'+sentItems.swap_id).
            innerHTML = "Sent!";
    },
    failure: function(o)
    {
        alert(o.statusText);
    },
    click: function(url,id)
    {
        sentItems.swap_id = id;
        var cObj = YAHOO.util.Connect.asyncRequest
            ('GET',url,this,null);
    }

}

You must have a success and failure function (and they must be named exactly that) in order for this to work. That’s it.

  1. Thanks Travis, this was useful!

  1. No trackbacks yet.