Is jQuery really that awesome?

JavaScript March 11th, 2009 @ 10:03

There is (and has been for a while) a lot of buzz around the JavaScript framework jQuery. It’s even gone so far that people (mainly softcore developers or non-developers) have started referring to Javascript/AJAX features (animations etc) as “jQuery features” (maybe not that wording, but in concept).

I guess one reason for jQuery being so popular is that it’s pretty light-weight and contains both useful “core” functionality, as well as easy ways of creating transitions/animations. It’s got a pretty wide and deep range of functionality.

Prototype and YUI are my old friends

I’ve mainly been using Prototype the past years and think it’s an awesome library. It’s got great functionality for traversing the DOM, handling AJAX requests and so on. But doesn’t have any easy-to-use fade or scrolling effects. That’s probably a great advantage of jQuery: it can be used for pretty much everything within a site.

I addition to Prototype, I’ve been using YUI for animations, widgets etc etc. Truly a great library too, but maybe a bit too heavy…

Now I’m not saying that Prototype in combination with YUI is the perfect solution. It’s a good enough solution, given that Prototype’s core features combined with YUI’s widgets are quite powerful. But combining several libraries can never be a truly good thing in the end, as it can get a bit confusing what to use where in the code. Also, it could make it more difficult to update to new versions.

It’ll be really interesting to see what YUI3 can provide us with, though! From what I’ve understood, it’ll contain all the nice widgets provided by today’s YUI, but slimmed down to less and more effective code. Maybe it can prove to be the ultimate library?

Don’t chain ’til you drop!

Now I haven’t been using jQuery to any large extent (yet), but there’s one thing that’s annoying me so far:

The overly excessive desire to chain everything into one line of code (OK, maybe that was exaggerating, but sort of…). Chaining code can be nice and make code more effective, but there’s only so much chaining you should do. Otherwise, your code will only be more and more difficult to read, while saving a few fractions of milliseconds here and there because you didn’t create a new var.

A colleague of mine came up with a good suggestion to only chain as far as the starting reference is needed. Meaning, if you’re performing actions on an element, you should only chain until you stop using that particular reference.

Example of some crazy chaining:

// clear foo, update text, create a clone and update its text
$("foo").empty().text("foo-text").clone().text("foo-clone-text");

Instead, this is how it should be written to understand what it does:

// clear foo and set text
$("foo").empty().text("foo-text");

// create a copy of foo and set text
$("foo").clone().text("foo-clone-text");

Now, maybe that wasn’t a perfect example. But my point being that chaining is OK as long as it’s easy to understand in what context the code is applied. Otherwise you’ll just confuse everybody else, and in the end yourself as well.

So what am I saying?

All in all: jQuery is a great JavaScript library. I’m not buying the hype to 100% and I’m not liking it enough to say that I’ll start using it instead of Prototype just yet, but as their community is so large, there’s obviously a good chance that they’ll keep evolving to something even better. Large and good communities are often an important thing when it comes to these things.

We’re currently only using jQuery sparingly in a couple of our products, so I will have more to say after we’ve evaluated it a bit more.

When I chose to use Prototype over jQuery, I did it because it was considered the best library at that point. It’s interesting that it can change pretty quickly: today, jQuery is considered the best and fastest library out there. Maybe it’ll change again…?

A first glance at GWT

JavaScript March 9th, 2009 @ 15:03

Web applications of today are often required to be quite complex – AJAX offers lots of nifty ways of giving the user a great experience. But developing stable applications using lots of JavaScript code can be quite challenging. Even though there’s help from great libraries such as Prototype, YUI and jQuery, it’s tricky to keep the code bullet-proof and fully tested.
Also, it can be difficult maintaining a decent architecture as the application grows, i.e. how to implement an MVC solution.

There’s of course a few people out there trying to address these issue, one of which being Google Web Toolkit (GWT).
We’ve discussed GWT from time to time, but haven’t really made a true effort of delving into it. I decided to try to find out how it really works and if it’s something for us to use in our applications.

Do we really need it?

I guess the first question you need to ask yourself is if you really need anything like GWT. Is our goal to build a full-feathered AJAX site without any browser reloading of pages? Do we want something with features like Gmail (from dialogues to rich-text editors)?

After researching GWT for a while, my feeling is that you either fully implement it (with effects on server-side and all) or you stick to using “old, self-developed” Javascript code. If you’re to have any kind of client-server communication (which you are), you’ll probably have to make some adjustments on the server-side as well.

Anyway… I tested a sample implementation usin GWT and as a Java fan, I was of course enjoying writing nice, strict Java code with full IDE support instead of regular JavaScript. There was really nothing to complain about when it comes to writing GWT code and it was fairly easy to get started and the compilation was quick enough and provided a nice debugging tool.

Are there any limitations?

My main concern is that using GWT will come with some limitations:

  • What if I don’t what to write the code exactly like it’s done with GWT? Will I have to write all my code using GWT?
  • How painful is it really to mix “regular” Javascript with GWT code (they offer a way of doing it, but it doesn’t seem like something you’d really want to do)?
  • Is it really stable enough for an entire application/game? To be honest, the GWT site itself is actually quite buggy…

One nice feature that comes with GWT is the support for creating modules. It seems quite easy to create a smaller module (perhaps a text-editor) and plug it in to your application. I’d say that doing so would be a first good test of GWT, as it still feels a bit scary to rely on it for an entire application (i.e. creating entire pages, browser history states etc).
Maybe using it for smaller modules the best use of GWT in our case? It would be really nice if server-developers (Java guys) could implement advanced client-side features without being afraid to mess it all up… :-)

To sum it up…

I guess these questions never will be answered unless I/we actually try it out in a real implementation (or at least a real-enough prototype). There’s obviously a whole lot of things with GWT that is really nice, but I wouldn’t feel fully comfortable relying on it as of today. Trying it out in a module or two would probably be the best way to draw some real conclusions.

In the end, it’s all about what you and your product really need.

References

Google Web Toolkit Website

GWT in Practice – Ebook (lots of reading)

Ext GWT Library (great widgets)

Unofficial GWT blog

Changing page without reloading browser

JavaScript March 6th, 2009 @ 13:03

I wanted to investigate if we can build web applications that doesn’t require reloading of pages in the browser, according to the principles of  Life above the Service Tier.

Evaluation

I started by reading the “Life Above the Service Tier” document, which was a description how it works today (and its flaws) and what the proposed solution is.
Also, I browsed the web for more articles, blog posts and comments on the issue.

Summary

The ideology (called SOFEA: Service-Oriented Front-End Architecture) is basically to put more rendering logic on the client side and only use the server side to transport data back and forth.

The principles of SOFEA could be implemented with help from AJAX. The controller resides on the client-side and is responsible for loading (XML) data and rendering the application using the received data, without actually reloading the page in the browser.
This means that JavaScript would be used instead of HTML/JSP to render the server data. Also, the server-side is much thinner and not really in need of any web framework (as all it does is receive messages and return XML data).

Conclusion

The principles of SOFEA seem very far away from today. There are several flaws with implementing in the suggested manor:

  • The web clients (browsers) of today are not good enough to handle could have problems handling as much logic as a large web site would require (given that old IE versions have so many users, ca 70-85%)
  • The client code (JavaScript) and architecture is not really mature/good enough to handle this much logic, given that it needs to be bullet-proof, easy to test etc (EDIT: unless using GWT or similar – it’s at least difficult to manage)
  • Developing applications would take much more time (hence be more expensive) as more manual labour is required to render content using JavaScript instead of letting the JSP engine do it with JSP/HTML/PHP/etc

Everything points to NOT implementing it in such a manor. At least not at this time, partly because the browser/client market isn’t ready for it.

Is there an other way?

Yes, there are other solutions to the main issue; changing pages without actually reloading the container (browser).

One suggested solution could be an extension to our model in Taikai, where AJAX can be used in combination with both XML, HTML/JSP and JSON, hence giving us the possibility to smoothly serve a full JSP page using AJAX.

A brief description of how it could be solved:

  • Make a JavaScript “controller” handle all URL requests to the server, in order to bind the answer to a certain action.
  • Set up “View XML files” for all views that are to be loaded with AJAX. The view could look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xmlRoot>
<title>Document title</title>
<styles>
<src>/css/foo.css</src>
<src>/css/bar.css</src>
</styles>
<scripts>
<src>/js/foo.js</src>
<src>/js/bar.js</src>
</scripts>
<content id="targetContainerId">
<![CDATA[<jsp:include file="foo.jsp" />]]>
</content>
</xmlRoot>
  • When returning the response from the server, the returning page would be the pre-defined XML (as per above).
  • The controller parses the contents of the returned XML file and performs its magic:
  1. Changing the document title
  2. Loading new styles and scripts (and possibly unloading old ones to keep the page slim)
  3. Updating the content element (with id=”targetContainerId”) with the provided content, which has been rendered by the JSP engine and therefore taking advantage of its power (i.e. JSTL, HTML tags etc etc).
  4. Updating the browser history using hash (#) technique, allowing for back/forward movement while remaining in the same page container

The main advantages with such a solution would be:

  • Allowing developers to create content using known and powerful formats, such as HTML and JSP
  1. Very important in respect to time efficiency
  2. Still allows for easier debugging and development
  3. No need to put too much logic on the JavaScript side, i.e. creating elements etc (although it could still be done in some components, if needed/wanted)
  • Still providing the user with the quicker experience of a site without (or with few) full page reloads

Remaining questions:

  • Would there be any risk of memory leaks, especially in older Internet Explorer browsers? And is there an easy way of eliminating these?
  • Is the support for “hash history technique” good enough in all major browsers?

References