Politics, Programming and Possibilities
29 Apr
I’ve been told that jQuery has been around since January of 2006. At some point during the last year, many developers within the Ruby community began to embrace it as an alternative to Prototype and Scriptaculous. Many of the programmer friends I look up to in the Merb community, specifically, are big advocates of jQuery.
So this post is an attempt at summarizing the strengths I’ve found in jQuery as opposed to the two other javascript libraries that I’ve worked closely with: mooTools and Prototype / Scriptaculous. My motivation for this is a soon-to-be-made decision regarding the javascript library that will be used in a browser-based application that I’m working on at a company in Chicago. The application needs a simple user interface and fairly powerful front-end. I’ve advocated the use of jQuery, and thought it would be useful to set out the reasons for my preference here on the InquiryLabs blog.
There is a fine set of documentation at jquery.com. Scriptaculous hasn’t seen much improvement in its documentation in over a year. Prototype’s documentation is (in my opinion) pretty, but still lacking.
It uses as its central paradigm the idea of “querying” the DOM for the desired elements and returning a collection (like a dataset, but with DOM elements) and then acting on the result, or set of elements. The reason it can be concise is that the element sets are chainable. For example:
documentreadyfunction
for var i = 0; i <= 6; i++
$‘h‘ + i + ‘ [@id]‘eachfunction
$‘<a class=”anchorlink”>\u00B6</a>‘
attr‘href‘‘#‘ + thisid
attr‘title‘‘Permalink to this headline‘
appendTothis;
;
;
(Hat tip, lucumr) In the above example, anchor tags are added to all headlines in the document. Note the clever use of chaining which reduces the size of the code, yet retains its expressiveness.
Whether it’s a simple function or an entire plugin, jQuery is built for extending. The ability to query for “element sets” and then act directly on those results is not limited by the core code. Since it’s so easy to add custom functions to element sets, it becomes natural. For example, I found that I often needed to add or subtract a value from a CSS property. I solved this problem by extending the jQuery core element set, like so:
var value = parseIntthiscssproperty;
return thiscsspropertyvalue + delta + “px“;
;
Using the above code, I can query for a set of elements and adjust the height property (for example) in one fell swoop. This will now add 5 pixels to the height of each “rack-item” element:
$‘.rack-item‘cssDelta‘height‘5;
According to Jeff Robbins and John Resig, the Drupal team chose it over Prototype because it is a mere 15k compared to Prototype’s 80k. The Dojo library is even larger.
Update:I decided to test this out myself to see what results I’d get. Resig’s comment was probably from jQuery’s earlier days when it was a micro framework. As far as I understand, it’s still possible to compress the jQuery library down to 18k or so, but that’s possible for Prototype, too. In fact, my tests show that un-gzipped, compressed code gets both down to 51k using Dojo’s ShrinkSafe online app.
Update 2:Thanks to Peter Higgins below for commenting on the size of Dojo. For comparison, its compressed (but un-gzipped) size is 77k.
Feedburner, Technorati, Drupal, Edgewall Trac, etc.
Here are some examples of the concise nature of jQuery, taken from the jQuery blog:
// AJAX Updater in Prototype:
‘placeholder‘url method: ‘get‘parameters: par ;
// AJAX Updater In jQuery:
$‘#placeholder‘loadurl + par;
// Note: This example doesn’t deal with the obvious
// iteration benefits we get if we wanted to load the
// response into every <p> object, for instance.
// Adding a class to an element in Prototype:
ElementaddClassName‘element‘‘className‘;
// Adding a class to an element in jQuery:
$‘#element‘addClass‘className‘;
// Adding a class to a group of elements in Prototype:
$$‘.element‘eachfunctionnode
ElementaddClassNamenode‘className‘;
// Adding a class to a group of elements in jQuery:
$‘.element‘addClass‘className‘;
“That last one is the clearest example of the difference in methodology. Because jQuery is passing messages to jQuery objects, the code is barely changed. jQuery doesn’t care that we’re now adding a class to a group of objects instead of one object; the underlying code is the same (add the class to the set of elements in the object). Prototype, on the other hand, requires an iterator.”
“And as your code becomes more complex, jQuery’s scales easily, while nested loops become the norm in frameworks like Prototype.”
29 Apr
Extraordinary as it may seem, my loving and persistent wife, Kelty, has finally graduated from BYU! I’m so happy for her, and relieved to finally see her on the other side of the divide. Now we just have to get me over there
Here’s a 3-minute video I made for her. Feel free to share this moment with us and send her your thoughts!
6 Apr
I just love being there to capture on video what Kelty does. Watch for a segment in this clip when Pedro (Kelty’s horse) rears up, and she masterfully keeps everything under control.
5 Jan
Here’s a tribute to my wife that I made from our video footage. You’re awesome, Kelty! Thanks for teaching me a bit too
P.S. The video is also available directly on youtube here.
3 Sep
This was a song that my mother in law, Ginger, heard when she was younger. She later named her last child and my wife, Kelty, after the reference in it:
oh the days of the kelty dancers
oh the ring of the piper’s tune
all for one of those hours of gladness
gone, alas, like my youth too soonwhen the boys begin to gather in the glen of a summer’s night
and the kelty dancers will be filled with joy and utter delightoh to dream of it
oh to think of itoh to dream of it
fills my heart with tears
It was a beautiful thing to hear Ginger sing this the other night. I seem to be able to find the things I post here, so I’ve posted it since I never want to lose it ![]()
31 Mar
It’s spring time and the weather is starting to beg me to go outside! I went for a bike ride this afternoon and quite enjoyed myself. It was almost like I was 10 again—I biked through the now empty yard where a house once stood (it burned down about 6 months ago) and watched kids playing in various parking lots (I live in an area with quite a few apartment complexes). A couple of neighbor kids seem to have gotten their hands on a couple of gas-powered remote control race cars. All of their friends were gathered around to watch the duel. That looked like fun!
I hope this finds all of you well. Good luck to those of you finishing off the semester at school. Endure to the end, eh? ![]()
28 Jul
It’s been almost two months since my last post, and that makes it about time I wrote again. I’ve been silent on this blog primarily for three reasons:
I will post about each of these items separately.
25 May
An old friend of mine, Kevin Yockey, and I were chatting about life, jobs and responsibility. He mused that I hadn’t become a game programmer like we once thought. Instead, I became a web developer. Sometimes I feel the weight of responsibility as an income-earner, and realize it was probably for the better that I travelled this path. Kevin’s wisdom (increasing with the years) shone through with his next remark:
It’s a lot easier when you’re in school and don’t have to worry about making money. But if you can make it doing something you enjoy then you’ve found the perfect line between responsibility and childhood.
Isn’t that the case?
24 May
At the beginning of this year, I announced Family Connection, an open source, free, and easy-to-use family website built using Ruby on Rails.
Now it’s available as a hosted, very easy-to-set-up web app called Family Anywhere! It has the same Family News and Address Book features as the original, plus a new Photo Album that lets you browse all of the photos you’ve shared with your family. The hosted version is $9 per month, or less if you buy a year at a time.
For those who would like to just check out how it feels without going through the 2-minute sign-up process, log in to FamilyAnywhere.com/test and use “test” as the sign-in name and “password” as the password.
I’m proud to bring this program out in to the light of day, as it reflects some of my own desire to promote a better, more family-oriented world. I hope you enjoy it as much as our family does!
6 Apr
In January of this year, James Lovelock, author of the Gaia Theory announced that the earth has caught a fever manifest by irreversible global warming.
More recently, the BBC released an article confirming the alarming rate of increase of CO2 in the atmosphere.
And now there is evidence that not only have our American governing officials been aware of it, but they have actively been censoring communication from those who can help us most. More information on this censorship here.