Archive for August, 2006

Javascript Toolkits: Prototype Versus Dojo

Tuesday, August 8th, 2006
If you deal a lot with writing Javascript in web pages, you'll often find yourself writing a lot of common methods over and over again, or creating a basic toolkit for yourself that contains a lot of utilities you like to use.  I end up writing a trim() method for getting rid of spaces before and after strings, cross-browser methods for getting browser dimensions and elements on the page, and utilities for making Ajax calls that works on all major browsers.

That all stopped once a couple of interesting toolkiits came into play:  the Prototype Javascript Framework and the Dojo Toolkit.

Both toolkits are libraries for Javascript that help in building applications.  Both serve to take a lot of the fuss of dealing with different browsers away from the developer and both provide a pretty interesting set of widgets you can use in your code to make some pretty interfaces.  So which one is better? 

It all boils down to personal preference, really, but after using both frameworks I definitely prefer Dojo over Prototype.  Before I get into the reason I like Dojo so much more, let me briefly describe both libraries.

Prototype

Prototype is very lightweight and is used in the excellect effects library script.aculo.us, written by Thomas Fuchs. There are also some pretty good widgets written with Prototype called Rico.  Script.aculo.us and prototype are built into the popular framework Ruby on Rails, and in fact if you use Prototype you'll find that your Javascript code starts to look a lot like Ruby code. 

One of the most used and well-known feature of Prototype is the $() function.  Imagine that you are wanting to get a reference to a certain element in the DOM in order to see what its value is.  Normally, you'd do something like this:

var myElement = document.getElementById("myElementId");

The $() method is shorthand for the document.getElementById part with some added benefits.  You can get the element by doing this:

var myElement = $("myElementId");

If you want to get multiple elements, you could do this:

var elements = $("element1", "element2", "element3");

Then elements would contain an array of references to your element, something the standard document.getElementById() cannot do.  There is also another method, $F(), which is like the $() method, but instead of returning the element itself, it returns the value of the element, making life very easy.
 
If you want to see more, I recommend looking at Particletree's Quick Guide to Prototype, and the Prototype documentation.

Dojo

If Prototype is a lightweight Javascript library, then Dojo is a heavyweight tool.  Dojo has a wide variety of tools available, all seperated by different namespaces that can be dynamically loaded whenever you need them.  This has the benefit of not cluttering the main namespace like Prototype does, so you don't have to worry about the library stepping on your own functions. 

To use Dojo, you unpack the downloaded zip file to a web-accessible directory, and include a script tag pointing to dojo.js:

<script type="text/javascript" src="/dojo/dojo.js"></script>

Then to include a specific package, do the following:

<script type="text/javascript">
    dojo.require("dojo.widget.DropdownDatePicker");
</script>


That includes the package dojo.widget.DropdownDatePicker, which is obviously the widget for displaying a dropdown date picker.

(BTW--Dojo has an equivalent to $(), dojo.byId(), and if you want an array you can do dojo.byIdArray().)

In addition to general language utilities, Dojo provides tools for dealing with all sorts of things.  Events, math, cryptography, persistence of data via Dojo Storage (yes--client side storage via Javascript! woohoo!), and a really nice system for creating your own widgets. 

The base widgets provided with Dojo are pretty nice, too.  On their homepage there is a "see it in action" link that has a list of demos for all their widgets, and also has a few sample applications.  Very neat stuff if you ask me.  If you can't tell, I particularly like the date picker widget, which can show up in a dropdown like this:



While there is a lot more to Dojo and it is criticized as being "heavier" than Prototype, the fact that there is no namespace pollution, heirarchical package management, abundant pre-written utilities, and a comprehensive set of existing widgets makes it a clear winner in my book.  If you want to see more, check out the Dojo manual.

The Beast Lives

Tuesday, August 8th, 2006

 We got the last set of components for our new computer and got it assembled and everything installed.  Here’s what we finished the system out with:

  • Samsung SH-S162L DVD Writer (it can do lightscribe, woot!)
  • Mitsumi 3.5″ floppy drive (needed for installing third party RAID drivers in Windows.  Yuck.)
  • 3 160GB Samsung 7200 rpm SATA 2 hard disks (using hardware RAID 5)
  • 2 1 GB PC3200 G.Skill SDRAM sticks with red heatshields
  • Windows XP Media Center edition (I got a really good deal on a full copy)
  • A logitech headset for online gaming goodness

We also had to end up getting some miscellaneous parts to help assembling everything:

  • A rounded IDE cable for the DVD writer
  • A rounded floppy cable for the floppy drive
  • a Lian Li 3.5″ to 5.25″ drive bay convertor (for the power supply wattage readout)
  • black cable ties for cable management

We also bought some split black flexible tubing to wrap the power supply connector cables in.  It makes the case look really sweet when the LED lights are on.  I’ll have to take some pictures so you can see what I mean.  We also ended up using some existing components we already had:  Speakers, a logitech keyboard and mouse, and our Samsung 913v 19″ LCD monitor. 

The Games

So I installed the OEM games that came with the video card, and of course they all suck.  It even came with King Kong, but I don’t think I’m going to play it. The visuals kind of stink and the game play doesn’t feel very fun to me.

Lori and I installed a few games that will keep us busy for quite some time:  World of Warcraft, Prey, and Battlefield 2.  We’ve been able to jack the video settings on all these games to the max and they look absolutely beautiful.  Playing World of Warcraft on this system is a completely different experience than playing on my iBook or my Mac Mini.  I can go into the busiest parts of Ironforge or Stormwind and not have any video lag whatsoever.  It is as smooth as butter.

So I guess if I’m not blogging much in the future, it is because of this new toy.  I’m happy that we were able to build a pretty decent gaming system with our budget, and it only took us a couple of months to do it.  I highly recommend building your own system to anyone who feels comfortable around computers.  It is definitely a learning experience.