Why I moved from Prototype to jQuery
jQuery is a JavaScript library which follows unobtrusive paradigm for application development using JavaScript. jQuery inherently supports Behavior driven development and is based on traversing HTML documents using CSS Selectors. On the other hand, Prototype is a JavaScript library for Class driven development which makes life easier working with JavaScript. Prototype library has a good support in Ruby on Rails via helper functions.
I have always used Prototype library for most of my projects until I was introduced to jQuery three months back ... and it enchanted me.
The reasons
- Behavior driven development (BDD)Using jQuery, behavior(s) of HTML elements is defined separately from HTML code similar to defining style of an element using CSS. Lets look at a simple example to display alert box on click of an element
1 2 3 4 5 6
$(element).click(function(){ alert("warning"); //fill stub for confirming this action from user });
A complex example: All elements of class "speciallinks" should emit the following behavior:- change their href link to "javascript:void(0);"
- generate logs on click
- onhover should change background color.
1 2 3 4 5
$("div.speciallinks").attr("href","javascript:void(0)") .click(function() {console.log("div.speciallinks clicked");}) .hover(function(){$(this).addClass("hovered");}, function(){$(this).removeClass("hovered");});
- MVC + JMVC framework divides web development into 3 separate parts Model-View-Controller and Ruby on Rails is a great MVC framework. The View part of MVC framework comprises of HTML, CSS and JavaScript which is bulky, combining three different parts of GUI development into one. Moreover, using Prototype helpers in Ruby on Rails views messes up HTML and JavaScript together. Here, jQuery fits in nicely (due to BDD) to separate JavaScript(J) from Views(V) to visualize the framework as MVC + J which I find very powerful especially working with Ajax.Using jQuery, I keep all my HTML files are clean and clear as all the JavaScript code is kept in .js files defining behavior of HTML elements.
- Chaining of actionsChaining of actions for a HTML element follows DRY principals and increases readability of JavaScript code. If I want to add a bunch of operations on a single/multiple elements, it can be as simple as:
1 2 3 4 5
$("div.message").show() .append("Action has been executed successfully") .addClass("flash"); // chained functions can be on separate lines :)
Now, this is possible because every method within jQuery returns the query object itself on which further methods can be applied to form a chain of methods on "selected" HTML elements. - CSS Selector rocks!CSS Selectors are very powerful when playing with HTML DOM. jQuery is based on CSS Selectors to identify elements in a HTML document, which avoids tedious job of managing idattribute for each of my HTML tags. Most of id attributes can be avoided using right CSS Selector. Prototype does supports CSS Selectors via $$ function, but it doesn't fully leverages the power of CSS Selectors. I find Prototype working best with element's id attribute.
- No more checks for presence of an elementIn prototype, I always need to check if an element exists before applying an action to it. For example: I want to display user specific content in a div{id='user-box'} only if user is logged in (this div will exist on rendered page only if user is logged in). In Prototype I will do :
1 2 3 4 5
if ($('user-box')!=null) { // this if block is redundant with jQuery $("user-box").style.backgroundColor = "red"; }
- Aids development processUsing jQuery HTML code is clean and nearly untouched. My web designer can easily modify html and stylesheets without learning the Prototype library.
I miss prototype
- On Ajax request, when a div is updated with a partial and this partial contains elements with JavaScript behavior, the behavior needs to be activated/reactivated. In jQuery I do not have to write a lot of code for this, but still I have to keep this in mind every time a partial is loaded with Ajax. In Prototype, each HTML element use to contain the corresponding JavaScript code along with it in the partial.
- Ruby on Rails helpers for Prototype library are awesome and makes life a lot easier working with JavaScript and Ajax.
- Prototype library provides Ruby like syntax in JavaScript for functions on arrays, objects, etc which further makes development easier and faster.
- With jQuery, element behavior gets activated only after DOM has been built and JavaScript files have been downloaded. For slow connections, this can be painful as there is a delay in activation of behavior.
For your first problem, see http://api.jquery.com/delegate/ and http://api.jquery.com/live/
ReplyDeleteThey attach an event also for the future!
Good choice ;-)
ReplyDeleteGlad you switch to the other side.
ReplyDeleteRegarding your point 1 of missing things, you should look into the 'live' jquery method, does just that.
there are occasions where using more than one javascript library is ok, jquery syntax is a bit confusing for large applications, but can be used as the visual manipulation layer, it is all javascript at the end of the day
ReplyDeleteThank you for your enlightening explanations. You are not the only one doing that step: There is a general movement away from Prototype, mostly towards jQuery: http://w3techs.com/technologies/changes/js-prototype
ReplyDelete