Beginning to play with MochiKit

I haven’t written any JavaScript for years. The only lingering memory was of pain–pain to find information, pain to use the API, pain to debug… There were probably a few more pains, but I’ve managed to successfully repress those memories. So, why return? Well, I want to some day be able to debunk the AJAX hype in any discusion I partake in :-)

MochiKit is used in turbogears to make the life of a “web developer” easier. Given the comments on the turbogears site I tought I’d take a quick look at it… I was surprised. There was a lot less pain this time.

Of course I started with some silly experiments. First off playing with the DOM tree a little. The HTML part ended up like this:

    <title>Experiment 1</title>
    <script type="text/javascript"
    <script type="text/javascript"
    <script type="text/javascript"
    <script type="text/javascript"

  <table id="theTable">
  <a href="#" onclick="addEntry();">Add entry</a>

  <div id="theText">
  <a href="#" onclick="addText();">Add text</a>

Nothing really exciting there. Here’s the two toy functions:

var i = 0

function addEntry() {
    var row = TR(null,
        TD(null, 'Hello'),
        TD(null, i)); = 'row' + i
    appendChildNodes('theTable', row);
    i += 1;

function addText() {
    var txt = getElement('theText');
    if (txt.text) {
        txt.text += ' Hello';
    } else {
        txt.text = 'Hello';
    replaceChildNodes(txt, P(null, txt.text));

Wonderfully easy to navigate and modify the DOM tree.

Well, this was somewhat silly. Hopefully there’ll be something more interesting to write about soon enough.

Leave a comment