Climbing the JavaScript Client Stack
JavaScript (JS) is fast expanding from a client side script to a platform for full feature web applications. The turning point seems to have been the availability of powerful JS libraries and the maturity of patterns that produce maintainable JS code. In a sense libraries like JQuery (JQ) almost changed the fate of JavaScript. Here I discuss some of the problems you have to face putting together HTML5/JS app and JS libraries that solve them.
JQuery or… uhh something else
I start by listing JQuery as its own because well that’s the decision, to JQuery or not to JQuery. JQuery is almost ubiquitous in the world of JavaScript web programming. There is no simpler way to handle HTML DOM events, DOM manipulations and basic Ajax requests. Once you learn the syntax of selectors it’s extremely easy to select what you want from the view and bend it to your will. The other important point is that the JQuery community is ENORMOUS. There is great documentation, many many plugins, and utility libraries built on to of JQuery most of which piggy back on jquery’s selector functionality providing beautifully clean syntax. Some of the libraries I mention in this article depend explicitly on JQuery and I for one am completely ok with that.
Interesting libraries that are not JQuery:


Prototype.Js for example is the go to JavaScript library for rails applications. It’s called prototype because of the preferred way it injects behavior in to objects using the JS prototypes. This enables some neat syntax that a ruby developer might love but can be problematic in JS. Modifying an objects prototype to inject new functions or change the behavior of standard DOM functions can have interactions with other JS libraries. That’s not playing nice. Dojo and Ext are full toolkits that overlap with JQuery features, but EXT for example has a licensing fee. Dojo doesn’t, but both toolkits encourage you to use them for exclusively for features like MVC, Widgets and Visualization. Don’t get me wrong, these toolkits are incredible; Dojo has a particularly large community. Ask yourself how much control you want. I prefer to stick to JQuery, and piece together a stack I find useful.
Modularity & Performance
When you first transition from a structured C-style object oriented language to JS you may not immediately see how a JS application could ever be as readable or maintainable as what you’re used to. Dependencies between sections of JavaScript seem almost hidden and sequencing script tags in the head section of your HTML page feels clumsy and slow. Until recently browsers did not even download script files in parallel. Through the use of script loaders you can encourage clean JS patterns and asynchronously load your JavaScript, with a readable declarative syntax.
Interesting Script loaders include: (The YUI & dojo toolkits include a script loader)
I particularly like require.js because it encourages use of pattern called Asynchronous Module Definition (AMD) and very modularized JS. Consider the following Syntax:
//Explicitly defines the "foo/title" module:
define("foo/title", ["my/cart", "my/inventory"], function (cart, inventory) {
//Define foo/title object in here.
});
Here a module called “foo/title” is defined. It has strict dependencies with cart.js and inventory.js. Whenever “foo/title” is referenced somewhere else require.js downloads this file, detects the dependencies, downloads them in parallel and executes all scripts in the correct sequence. Essentially it builds a dependency tree and downloads everything necessary and executes the scripts in the tree from the bottom up.
AMD has now gained traction with the JQuery community as JQuery now registers itself with AMD script loaders if you’re using one. Ultimately if you fall on heavy use of the JavaScript approximation of OO concepts like classes and inheritance you’re missing the point. JavaScript is a world rich with its own patterns and practices, rooted in functional programming concepts.
Patterns and Utilities
MVC & Templating
It’s very likely you’re going want to pick up an MVC framework to support a UI with clean separation of concerns. It’s much more difficult here to suggest a clear winner as each of the available solutions take a distinct view of MVC and have a learning curve. The easiest to get up and running with is probably Knockout.JS thou I’ve had the most experience with Backbone.
Interesting JavaScript MVC Libraries include:
&
& 
These libraries represent three distinct solutions. Backbone and Underscore work together while Knockout and JSRender do the same. Backbone uses underscore for utility functions and templating while Knockout uses JSrender for its templating solution. Of the three, javascriptMVC takes the most architecturally pure view of MVC. The strength of javascriptMVC is its powerful controllers which uses JQuery to bind to view elements directly mapping the JQuery events you know and love to controller behaviors. JavascriptMVC also has a nice library of prewired widgets and plugins, thou again I’d rather piece it together myself. Knockout is much closer to the MVVM pattern you use in WPF development. View models contain observable fields which can be bound to view elements directly in the HTML. The interesting bit is that they can be two way (something of an eventing super feat in javascript). If you’re a WPF developer this will feel pretty natural. Backbone is the lightest of the three. Traditional “View” functionality is spread over html and a View Object. Check out this basic Backbone View:
var DocumentRow = Backbone.View.extend({
tagName: "li",
className: "document-row",
events: {
"click .icon": "open",
"click .button.edit": "openEditDialog",
"click .button.delete": "destroy"
},
render: function() {
...
}
});
Notice the backbone view “class” has a tagName. It represents an list item element on the view. In the events section, events which are relevant to elements in the list item are handled. So here the view object blends a bit of traditional “view” and “controller” functionality.
Feature Detection
Feature detection is something you’ll have to consider once you start delving into HTML5 features. The three browser heavy hitters IE (mosaic based), Firefox (Geko based), and Chrome (webkit based) each implement their own subset of these features, so to ensure a cross browser future proof app you’ll have to detect individual features and work around them when they’re not available. I say “feature detection” and not browser detection for a good reason, new browser versions are coming out regularly while features are part of a spec the browsers are building towards. You won’t need to change your code to support a new browser if your feature detecting.
Interesting Javascript Libraries Include:

Has.js and modernizer.js both have the same kind of “if(feature)” style syntax. Consider this bit of Modernizer code:
if (Modernizr.webworkers) {
var worker = new Worker('scripts/workerScript.js');
worker.addEventListener(...
worker.postMessage(...
}
else
{
require([“scripts/workerScript”], function (worker){
worker.dowork();
}
}
Here I test whether this environment supports HTML 5 web workers. If it does we use them and improve the performance and responsiveness of page. Otherwise we default to do doing the work on the same thread still asynchronously loading it with require.js and telling the same module to do the work. Neat!
Check out Part 2 for discussion on Widgets and Visualization, Instrumentation, and Testing!