I wrote a rant about something that annoyed me greatly when working with JS libraries.
read the rant if you want. Or read below, which is the same thing, but in a nicer and hopefully clearer way.
Because about half of what I ranted about was wrong, and the other half wasn't worth getting angry about. It's just slightly annoying.
A great deal of what I do with js libraries is move page elements around, change their size, and toggle them on and off.
Every JS library has slightly different ways to do this, and these ways aren't internally consistent.
(By "internally consistent", I mean that functions which get a value should return data in the same format accepted by functions that set that same value.)
This isn't too surprising, considering that the properties in question are really css. JS libraries don't like to layer tons of abstractions on top of css, prefering to simply let the programmer get and set attributes.
But for the things I described above (element position, size, on/off), css attributes aren't the greatest. I like to store visibility as a boolean (not "none" or "block"). I like to store and manipulate position and size as integers (pixel dimensions). As great as css is for defining sizes in percentages or ems, my javascript is only concerned with their pixel size.
For a long time, I've been a big fan of the <a href="MochiKit</a>> js library, because it makes what I want easy.
I recently learned that jQuery does this too. Thanks John!
Other js libraries don't measure up as well:
| |
Set Element Position |
Set Element Dimensions |
show/hide element |
| MochiKit | :) | :) | :) |
| JQuery | :) | :) | :) |
| YUI | :) | :( | :( |
Prototype/ Scriptaculous | :( | :( | :) |
| MooTools | :( | :( | :) |
I hope those libraries improve at some point. For the time being, I'm stuck writing wrapper functions for setting element position and toggling visibility whenever I work with them :(
First, some sober background:
I've done a fair amount of work with Prototype, YUI, and MochiKit, both for work and personal projects.
I've used a smattering of MooTools and JQuery.
Update - Thanks to John Resig for showing how to do what I want in jQuery. Yay jQuery!
FROTHING RANT BEGIN !!!
You know what I do a lot? Toggle DOM elements on and off. Move them around the screen. Change their width and height.
Here, for example.
I do not want to mess with css to do this.
Call me crazy. I want to represent visibility with a boolean. X and Y coordinates with integers. I can do math on that. No css while I'm doing math! They are not belonging together!
That's why I like MochiKit - Because it makes what I want easy.
Bully for MochiKit! But what about other, more well-known JS libraries?
| |
Set Element Position |
Set Element Dimensions |
show/hide element |
| MochiKit | YAY | YAY | YAY |
| YUI | YAY | FAIL | FAIL |
Prototype/ Scriptaculous | FAIL | FAIL | YAY |
| MooTools | FAIL | FAIL | YAY |
| JQuery | YAY | YAY | YAY |
WHAT THE HELL IS WRONG WITH YOU.
Several of these libraries have functions to get the position and dimensions of an element as nice numbers. But no corresponding function for setting them! Go play with css attributes, ha ha!
I'm perfectly able to write wrapper functions that let me do visibility toggling and positioning. But I hate, hate, hate being forced to do this. The point of a library is to standardize and optimize common tasks.
Especially YUI. Jesus, what are you guys thinking? It's like a rewrite of java in javascript.
Prototype, I figure you'll get this fixed first. You know what regular developers want. You're from the streets. Nice job fixing up the event model with currying and binding. You're almost better than MochiKit.
MooTools, same deal. You can kick Prototype/Scriptaculous' ass because you're not two separate projects. So do it already.
JQuery, never mind, just keep doing your thing. You're completely about the css anyway. You aren't interested in doing math. I may not use you very much :)
Rant complete! Thank you very much for reading.
Feel free to point out if I'm an idiot for overlooking some easy way to do what I want any of the spots marked as FAIL. But if it involves setting css attributes, forget it. Booleans and integers.
I'm perfectly capable to write wrapper functions that let me do visibility toggling and positioning. But I hate, hate, hate doing it.
Do it once and put it in a file like we all do. What's your problem?