Thinking About Switching to jQuery?

jquery.pngI was pondering the question too – fortunately not for long! I am in the jQuery camp now and couldn’t be happier about it. Now, I am not saying it is for everyone, neither that it is superior to prototype / script.aculo.us or anything like that (neither with nor without Rails) – all I am saying is that I switched and didn’t regret it! As my earlier research based on Rails Rumble suggested, more and more people are switching to jQuery (as well as companies like Microsoft and Nokia), so why couldn’t you give it a spin? Here are some articles and stuff I found useful when I was in your shoes:

What’s the Great Idea?!

Coming from Prototype? Who isn’t…

Going Further

Tools

Books

  • jQuery in action – I am reading this book at the moment and it totally kicks ass.

What are you waiting for? Enter the red tower of Hanoi – you won’t be disappointed!

20 thoughts on “Thinking About Switching to jQuery?

  1. As you guys, I prefer jQuery over Prototype, but there’s one place which is less obvious — classes.

    What I like in Prototype is the simplicity of defining classes to group functionality (components, behaviors or simply related functions). In jQuery we do it with plug-ins, which is fine and even superior approach at times, but “private methods” is what I really don’t “feel” yet. If in Prototype I use classes and list the methods as any other element, in jQuery I have to use the vague concept of closures that looks more like a patch rather than a carefully planned feature.

    Can anyone debunk my misconceptions?

  2. Aleksey,

    I’ve found that once you have the pattern down, building “classes” in vanilla JavaScript is pretty easy. Prototype definitely makes the process more Ruby-ish, by handling initialization and inheritance quite pleasantly, but even these concepts (if you need them) are easy enough without the overhead.

    The basics:

    var TestClass = {
      attribute: "OK",
      method: function () { return "no"; }
    }
    

    …or…

    var TestClass = function (arg1, arg2) {
        this.arg1 = arg1, this.arg2 = arg2;
        var privateAttribute = "KO";
        var privateMethod = function () { return "on"; };
        this.publicAttribute = "OK";
        this.publicMethod = function () { return "no"; };
        // ...do other stuff for initialization...
    }
    
  3. Stephen,

    Thanks for code samples. These are generic ways and I ended up using the second approach in my plugins, since a plugin in jQuery is just a function. In the second case you almost have a jQuery plugin with few little corrections.

    I’m not sure how JS implementations in different browsers deal with this, but it seems to me in the second case we are going to have privateMethod and publicMethod on per-instance basis. For example, if you run the plugin over a set of selected elements, each element will have this piece of code associated independently (other wise how instance variables would work), and memory may be allocated excessively. However, it certainly can be that it’s clever enough to have code globally while data separately.

    You gave a good reminder of what Prototype did and what I currently miss — initialization and inheritance. I wonder if there’s a place for such things in jQuery. Not that I’m pulling my hair out looking for a way to have it. Just curious.

    Thanks again. Cheers!

  4. Let me be the dissenting voice here. I tried out jQuery spicing up a new project this year and was quite happy with it. However, in another project where the whole interface is highly JS- and Ajax-driven, the code seemed to turn ugly pretty quickly. I jumped back to Prototype (for the reasons Aleksey mentions above) and the codebase has since grown considerably, still staying as nice and tidy as one would expect on top of a kickass Rails app.

    For the reasons mentions above, I still prefer Prototype for JS applications (as opposed to sprinkling some behaviour to a mostly static web page). Your codebase will in my opinion stay much cleaner and more maintainable using Proto. This means, however, that you as a developer must be a competent OO programmer. I fully understand how a designer-come-UI-programmer would prefer the one-object-rule-all approach of jQuery.

  5. Pingback: Web 2.0 Announcer

  6. nice overview. i find rails and jQuery kindred in spirit …the way mine feels when using em together. nothing against prototype or anything other lib – i was in jQuery land before rails, so it was easier to stay there. the selectors are so elegant.

  7. Pingback: links for 2008-12-09 « pabloidz

  8. Hi Peter,

    I am in the same camp as Jarkko above :) And quite firm that is :)

    The Prototype&Scriptaculous list has a nice discussion of precisely these topics: http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/8b3bbc96fbd5ad7

    For me, the issue is not the speed/easiness I can “sprinkle” (as Jarkko) writes some behaviour over the page. I regularly need some complex functionality in JS, eg. like this one: http://gist.github.com/14866 => Objects having state, etc.

    And let’s not forget the Prototype is essentialy a “rubyfied” JavaScript, which makes things like array traversal, regexp matching/replacing, very, very similar to what we are used to do in Ruby.

    I’d be very interested in any other argument for jQuery than it it’s “easier” or “faster” to add some dynamic behaviour to a page :)

    –karmi

  9. Of course, I am fully aware of one major argument pro: the myriad of jQuery plugins (http://plugins.jquery.com/)

    And I am also aware that Rails will ship with jQuery as default, eventually. That is the power of the inevitable .)

    (Sorry for broken language in the last comment, sometimes I type too fast, I guess…)

  10. I guess it comes down to whether you’ve done any object oriented work before or not. Whether you work with classes or not. Whether you’re writing everything with functions or whether you’re writing with methods. Whether you extend the String class or Array class or whether you prefer PHP because you don’t know much about the former. As it goes, good functionists become eager methodists but not without some hype and hurt along the way.

  11. @Joran: so you are basically saying jquery to prototype is what PHP is to Rails? That might be a bit strong :-)

    @karmi: I don’t think so jQuery will take over in Rails, if for no other reason, the heavy use of Prototype in 37signals…

  12. Pingback: Ruby, Rails, Web2.0 » Blog Archive » Add a powerful AJAX Table to your Rails Application in 5 minutes

  13. Pingback: links for 2008-12-14 « Brent Sordyl’s Blog

  14. @Peter: I’m not saying it’s analogous to what PHP is to Ruby. That might be a bit simplistic. :) I do think that at a certain point anyone would get tired of having their grandma pass on messages to other people on their behalf. If you want to speak to your brother, speak to your brother. Don’t ask your grandma to talk on your behalf every time you want to say something.

    That’s the problem with JQuery. You’re talking through the $ object. Prototype lets you talk straight to arrays. Or straight to strings. JQuery says “isn’t it fantastic that you couldn’t possibly make a fool of yourself by talking straight to these things?” Prototype says “sure but most of the time you wouldn’t make a fool of yourself anyway and is it really all that fun to be talking through your grandma like that?”

  15. Dear Ruby & Rails fans,

    no other framework has in recent years influenced the world of software programming as much as Ruby on Rails has. Within the last years, many masterminds of the industry have succumbed to Ruby on Rails’ charm. Imposing names have become Ruby ambassadors – creating awareness for this beautiful technology.

    We at S&S Media, the publisher of magazines such as Java Magazin, dot.net Magazin and PHP Magazin among others have always been passionate with emerging technologies and thus giving the market valuable impulses. Hence, we have started a magazine dedicated to the Ruby on Rails Community – RailsWay Magazin. (www.railsway-magazin.de)

    Now we established a European conference for the Ruby community!
    “RailsWayCon – the conference on Ruby, Rails and more…” takes place from May 25 – 27, 2009 in Berlin.

    We set up a world-class conference featuring the brightest heads of the Ruby on Rails community.
    RailsWayCon brings all actors – open-source developers, users and business strategists – with a unique concept together. Know-how transfer, experience sharing and business innovation will be possible.

    WE INVITE YOU – AS A COMMUNITY LEADER – TO JOIN THE MAIN CONFERENCE FOR FREE
    If you are interested to take part in the conference, please send us your contact details (full name, address, phone) via e-mail.

    In return, we would be glad if you place our banner for RailsWayCon on your welcome page and integrate information about RailsWayCon in your next newsletter.

    All information about RailsWayCon: http://www.railswayconference.com

    If you have any further questions, please do not hesitate to contact me.
    E-mail: pschwenk@entwickler.com
    Phone: 0049 (0)69 63008914

    We look forward seeing you in Berlin!
    Software & Support Media

    Patrick Schwenk

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>