GetGlue's Front End Stack

Earlier this year GetGlue began work on the new GetGlue website. It was a chance for a clean engineering slate, allowing us to put all our knowledge together to begin a new project on strong footing. It was a chance to use all of the most powerful client-side technologies to allow us to work efficiently and quickly.

Our old front-end stack consisted of a PHP backend that rendered XML documents with XSLT. We used vanilla CSS to style our pages and plain JavaScript to add sugar to the UI. We relied on jQuery for DOM manipulation and various jQuery plugins to increase the usability of the site. We optimized our JavaScript files for deployment via our own concatenation and optimization script and served it as one canonical file.

The new GetGlue website is taking advantage of many new powerful front end tools.

The tl;dr list of tools:

Preprocessors: CoffeeScript and SASS

Before any code was written we made the decision to use preprocessors as part of our tool-chain. Specifically we decided to use CoffeeScript and SASS to code and style the new GetGlue website.

CoffeeScript has enabled us to develop features at a break-neck pace. It strips away a great deal of the boilerplate code that exists in JavaScript. For example when referencing a property on a JavaScript object:

view = response.items[0].title

There'd be instances when the items array would be empty. With JavaScript we would have to verify its existence via:

if (response.items && response.items != null) {
    view = response.items[0].title
}

The CoffeeScript equivalent to the above code involves just one additional character:

view = response.items?[0].title

This is one reason we have found CoffeeScript to be incredibly powerful, enabling us to be twice as productive.

SASS has also allowed us to code efficiently and quickly. We are making great use of SASS’a nested selectors as well as mixins. Those two features alone have made coding CSS a joy again and not a pain. Through mixins we can keep our CSS more DRY-compliant, and with variables it is a breeze to update color configurations.

In addition to SASS we are using Compass. We have found Compass to be a great utility belt of common SASS mixins we would have otherwise made ourselves. Rather than having to manually type every vendor prefix we leverage a Compass mixin to automate the process. For example:

@include border-radius(4px)

Compiles to

-webkit-border-radius: 4px 
-moz-border-radius: 4px
-o-border-radius: 4px
border-radius: 4px

We get to save time and sanity. It's a wonderful addition to our toolkit.

Libraries

At GetGlue we also take advantage of a number of JavaScript libraries.

It almost goes without saying that we use jQuery for a whole slew of things. DOM selection, manipulation, and everything in between.

More exciting however is our great use of Underscore.js and Backbone.js. Our entire front-end UI has been built with Backbone Views, Models, Collections, and Routers.

Each page of GetGlue is comprised of one general PageView which in turn contains multiple sub-views (and usually those sub-views have sub-views - turtles all the way down).

All of our data is represented in Backbone Models and Collections with our views bound to change events to re-render themselves.

And naturally we employ underscore.js’ functions to make common tasks more enjoyable to perform. Varying from _.map to _.throttle - it makes our coding lives an absolute joy.

Mustache

Currently GetGlue is rendered entirely client-side and because of that we make extensive use of Mustache templates.

We've found the (mostly) logic-less Mustache templates easy to work with and they are a cornerstone of our new website. Without any templates we'd have nothing to render and there'd be no website for you to see.

By keeping (most) of the view's logic out of the template we're able to focus on just the structure of the view and not have to worry about anything else. This allows for fear-less template refactors as nothing else is affected.

RequireJS

When developing a large JavaScript application it can become hard to keep track and manage all the moving pieces that are required to make the application run. To that end we have turned to RequireJS as our module loader and build tool.

Each discrete section of code lies in its own JavaScript file, attached to its own namespace, and only included by another file when explicitly requested. By adhering to these strict rules we avoid unexpected behavior and can code with confidence.

Having code for one specific functionality in its own JavaScript file makes for great organizational clarity and lends a certain amount of intuition to our code base: by that I mean when file B is a subclass of file A the file structure mimics that behavior.

For example on GetGlue when you open an item from the Guide you open what we internally refer to as a Card. Each of those types of popovers are derivatives of a Card. In our file tree we have the card.coffee base class and in the directory cards/ lay all of the card subclasses.

This intuitive structure makes not only for development delight but also allows for easy on boarding of new hires as the system is easy to understand.

We also use RequireJS as our primary build tool for our JavaScript and SASS.

The way you include JavaScript files into other modules is through RequireJS’ require() function. As part of its build process RequireJS traces all require() calls and inlines them into one JavaScript file that we then serve to the browser. (Note: RequireJS allows for other build configurations).

RequireJS will also take all CSS import statements and inline them into a single CSS file.

By the end of the build process we’ve slimmed our JS and CSS files to one a piece making for a quick download by the client and a speedy experience.

Conclusion

We’re very proud of what we've accomplished with the new GetGlue website.

By switching to CoffeeScript and SASS we were able to develop quickly and efficiently. We were able to create features and tweak them without much effort. It made the entire development experience much more enjoyable.

Giving our site some Backbone.js made UI transition and state management easier than ever before. I will never again dip my hand into the DOM to find state. Thar be dragons.

RequireJS is a beautiful tool that I wish I had known about years ago. Its module management is intuitive and its build tool is very powerful.

We've had a great time building the new GetGlue website. We hope you enjoy it as much as we do.