jQuery Inspired Actionscript Library, part 2

Hah, didn't expect a part II now did you? Well, I was contemplating it too. So, here's why: last night I was thinking about how I would make the categories (groups of tags with a single name) section in OneLobby interesting, and somewhat different than the single tag view, so I decided I will show a category relationships section. The way this will work is that I will find similar categories (based on categories that use some of the tags of the current category) and then find similar categories of those categories. Only two levels deep. Then hopefully I will sort out all the unique ones, and find a nice way of finding links between them (efficiently). Finally, I'll output all this info to XML and flash with read it.

My AS library comes into play for building this XML into something damn fancy. Basically I will parse the XML (easy), display circles with text for each category (a la Digg Swarm, easy), and finally I will draw lines connecting all related categories (moderate). And maybe, (just maybe) if I am not underestimating this whole task, I can add some sort of movement to the whole thing so that its more interactive.

Now, the first version of the AS library didn't have all of the kickass stuff that jQuery had in the sense that jQuery does automatic looping of things, e.g.: $("//div,//span").setClass("moo"); (this will set the class 'moo' to all div's and span's). Well, now my AS thing can do that now (with the exception that there is no DOM in AS, but there are movie clips!). So, I can do $("movie1,movie2").hide(); and those two movie clips will be hidden, yay!

This time around I decided to go all spaghetti code! Woo for the a's and b's! Well, suffice to say, if you look at the code, you will find it hard to read. I'm also trying to stick to more of a jQuery-esque style of coding and simplifying functions to more complex functions. So, here's an example:

public function attr(k, v, z)
    return v != undefined ? this.each(function(i)
        if(k.constructor == Object)
            for(var p in k)
                this[p] = k[p];
            this[k] = v;
    }, z) : (this.size() > 1 ? function(e) 
        var c:String = '';
            c += this[k];
        }, z);

        return c;
    }(this) : (_o[0][z][k] || _o[0][k]));

You don't even need to pretend to understand that, but there are some funny things I've done in there.

  • The whole things is a single return statement.
  • One of the parts of the return statement is a self-calling anonymous function!
  • This function is super multipurpose.
  • You can barf now.
Heh, so it is pretty ugly, but it makes sense to me! The function is used for $.get, $.set and $.attrib (used for getting/setting attributes on XML nodes). This will probably be used elsewhere, but the whole thing isn't done yet.

If you want to see the code in all ugliness (sofar), check out http://flash.filearts.com/FileArts2/.

One last note is that this library, like the previous version, is completely chainable just like jQuery is. If it's not obvious how this is accomplished, the way I've done it is via a nifty feature of actionscript 2.0 classes called __resolve. When you create a public function called __resolve in a dynamic class, you can call functions that don't necessarily exist in that class. Since I store extensions of the FA.query class in other classes (that extend FA.query), I can call those on the fly without having to chunk everything into one class.