The power of a good toolkit

| 0 Comments
In my off time which I still have a lot of because I am on overhead at the consulting group that I work for, I've been learning JQuery and Prototype, though mostly JQuery. Today I refactored some of the JavaScript code that I wrote for a Movable Type plugin that I wrote using JQuery, and the following before and after is a good illustration of what a good toolkit can do for code that reimplements the wheel
var feedUrl;
var removeSelector;
        
function btnAddFeed_onClick()
{
    var ajax = createAjaxObject();
    ajax.onreadystatechange = function()
    {
        if (ajax.readyState == 4)
        {
            var parser = createXmlParser(ajax.responseText);
            var id = parser.getElementsByTagName("id");
            var title = parser.getElementsByTagName("title");

            if (id.length == 0 || id[0].firstChild == undefined)
                return;
            if (title.length == 0 || title[0].firstChild == undefined)
                return;

            var newElement = document.createElement("option");
            newElement.value = id[0].firstChild.nodeValue;
            newElement.text = title[0].firstChild.nodeValue;
            removeSelector.appendChild(newElement);
        }
    }
    ajax.open("GET", "<$mt:CGIPath$>mt.cgi?__mode=add_feed_mode&blog_id=<$mt:var name="blog_id" $>&feed_url=" + feedUrl.value, true);
    ajax.send(null);
}

function btnRemoveFeed_onClick()
{
    if (removeSelector.options.length == 0)
    return;
    var ajax = createAjaxObject();
    ajax.onreadystatechange = function()
    {
        if (ajax.readyState == 4)
        {
        var options = removeSelector.getElementsByTagName("option");
        var toRemove = null;
        var toRemoveValue = removeSelector.options[removeSelector.selectedIndex].value;

        for (var index = 0; index < options.length; index++)
        {
            if (options[index].value == removeSelector.options[removeSelector.selectedIndex].value)
            {
            toRemove = options[index];
            break;
            }
        }
        if (toRemove != null)
            removeSelector.removeChild(toRemove);
        }
    }
    ajax.open("GET", "<$mt:CGIPath$>mt.cgi?__mode=del_feed_mode&blog_id=<$mt:var name="blog_id" $>&feed_id=" + removeSelector.value, true);
    ajax.send(null);
}
The same JavaScript functions rewritten with JQuery and switching from XML to JSON for communicating data back and forth between the browser and web server:

var feedUrl;
var removeSelector;

function btnAddFeed_onClick()
{
    $.ajax({
        url: "<$mt:CGIPath$>mt.cgi?__mode=add_feed_mode&blog_id=<$mt:var name="blog_id" $>&feed_url=" + feedUrl.value,
        dataType: "text",
        type: "GET",
        timeout: 30000,
        error: function() { alert("Could not download and parse " + feedUrl.value); },
        success: function(json) {
            try
            {
                var obj = eval("("+json+")");
                $("#removeSelector").append("<option value='" + obj.id + "'>" + obj.title + "</option>");
            } catch (ex) { alert("Error adding feed"); }
        }
    });
}

function btnRemoveFeed_onClick()
{
    if (removeSelector.options.length == 0)
    return;
    var label = removeSelector.options[removeSelector.selectedIndex].text;

    $.ajax({
    url: "<$mt:CGIPath$>mt.cgi?__mode=del_feed_mode&blog_id=<$mt:var name="blog_id" $>&feed_id=" + removeSelector.value,
    dataType: "text",
    type: "GET",
    timeout: 30000,
    error: function() { alert("Could not remove " + label); },
    success: function() {
        $("#removeSelector option:selected").remove();
    }
    });
}

That's a lot more readable, and requires a lot less maintenance on my part.

Leave a comment

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Recent Entries

The three purposes of the federal income tax law
Businesses will spend about 3.4 billion man-hours and individuals about 1.7 billion hours figuring out their taxes this year.…
Progress of a different sort
You know we have reached a level of decadence seldom seen in the history of the West when our women…
And police wonder why the public rarely trusts them
But there is some good news to report here, too. The Maryland state law, as noted, is the first…

Subscribe

Advertisements

OpenID accepted here Learn more about OpenID