Thinking Big… Architecting a large application with jQuery / Backbone / Require, an Overview

A Little Background

Recently at work we’ve been in heavy development mode on a new thick-client application architecture.  If you aren’t familiar with the concept, thick-client essentially means putting most of the work onto the client’s processor, and removing it from the server.  The benefits of this are many, not the least of which is a much faster site response time for your users, and a much lighter load for your server.

To accomplish this basically means using javascript and a lot of AJAX, as well as implementing the application such that that a) search engines can still index your site and b) the user can both bookmark and use the back button wherever they are in your app.

In order to facilitate this heavy use of ajax and management of state, a number of MVC(ish) patterned javascript libraries have been created, and more appear seemingly every week.  After a little bit of research, we settled on Backbone due to its barebones nature, low overhead, lesser learning curve, and the size of the community.  It always helps to be able to reach out when you get stuck.

In combination with jQuery (we are already using it heavily), and Require.js (a dynamic resource loader which has also been in use for quite a while), we had the perfect trinity of tools to get the job done.

Basic App Structure

There are a lot of things to consider when architecting a large application.  For HMS, this is compounded by the fact that a) we have a lot of users with a lot of different site configurations, b) we have different variations on the main backend application, c) there is a lot of legacy code and newer front-end logic that needs to maintain compatibility so that pieces can be added one at a time.   This is where Require and its support of AMD (asynchronous module definition) really shines.

All of our thick-client application code is managed through AMD, and there are two things about it that are very awesome:

  1. It allows seamless integration on a module-by-module basis with the current application code
  2. Even portions of modules can be broken out and used before their parent module is complete – for example, we already have the autocomplete module live, even though the search module is still in development. When the time comes, it will be trivial to move it back into its proper place.

The general application structure goes like this:

  1. Event Aggregator
    Outlined here, we have a single global object, and its job is to manage communication between modules.
  2. Outer Router
    We have two routers.  This helps keep the application router a lot simpler, and keeps module related logic inside the module. The outer router manages routes for the entire application, and determines which modules to load.
  3. User Module
    Right now, this is the only module that sits everywhere in the application. As such, it is loaded by the outer router.
  4. Modules
    Each module contains its own router as well as templates and views. The router loads the views, the views load the templates (we’re using Hogan).  When we compile with require, each module (including templates) becomes a single file (the module router).
  5. Models
    The models are the glue between the client and the server,  and they sit in their own folder because multiple modules may use the same models.
  6. Form Mediator
    This is actually one of the modules, but its worth describing here.  We use a special Backbone view to handle every form, enhancing functionality as we go.  This view simplifies forms immensely, managing the model, validation, and state with ease.  Its special power is to allow multiple modules to combine into a single form definition – most usefully for search / advanced search.
  7. UI Modules
    Most DOM manipulation (outside of insertions/removals)  and all UI effects are offloaded into separate UI modules.  The reasons for this are a) it separates presentation concerns and b) it allows for a much easier time should we decide to switch from jQuery for future manipulation (one can dream of full standards support across browsers)
  8. Backbone.Store (localStorage)
    Of course we’re using this for our app.  Currently it persists forms, helps maintain the user session client-side, and caches model data.

Obviously, there are many ways to architect a thick-client application.  Our approach intends to prevent module dependency (outside of UI Modules), separate DOM manipulation from data logic, keep communication centralized in an event aggregator, load quickly (through smart module loading and use of localStorage, among other things), and preserve the global namespace through the use of AMD.  There are a lot of details I’ve left out (for instance reuse for mobile), but I do hope to dive a little deeper in future posts.

 

 

Backbone Resources – Become a Badass Chiropractor.

In the process of trying to get the dev team here at Homes Media Solutions up to speed on Backbone, I’ve been collecting links to a diverse range of articles, resources, etc. on the subject. Following is a curated list of what I believe are the best resources for learning Backbone and being badass at it (in no specific order).

  1. Backbone Fundamentals
    Open source e-book, started, curated, edited by Addi Osmani.  Still being written, but in time sure to become the defacto free reference to Backbone development.  There are lots of other good resources at the bottom of this page.
  2. Event Aggregator using Backbone
    An elegant solution to a common application problem.
  3. Data-binding with Backbone
    Derek  Bailey (see #2) is awesome.
  4. Understanding MVC And MVP (For JavaScript And Backbone Developers)
    Addi Osmani’s post on the history of the aforementioned patterns, and how they relate to the Backbone world.
  5. Recipes With Backbone ($24)
    Okay, this one isn’t free, but it goes through some useful Backbone patterns for dealing with common application architecture concerns.
  6. Peepcode Backbone Screencasts ($12)
    If you are a total Backbone beginner, these screencasts are worth every one of those twelve dollars.  Peepcode has a few Backbone related screencasts on their site, so don’t just settle for the basics (which is linked here)

And here is a bonus link because it’s worth looking at and following the links that branch off of it:

Javascript Patterns Collection  by Shi Chuan – a list of patterns and anti-patterns to make your javascript awesome.

Introducing Backbone.Store … JSON storage for Backbone.

Right now, you can’t do much better for quickly developing javascript applications and modules than the triumvirate of  Backbone, jQuery, and Require.  Each of these libraries fit together so well (IMO), and have increased my personal productivity immensely – improving quality, performance, readability, and consistency.

There was already a great library for Backbone to utilize HTML5 localStorage functionality; however, there were a few drawbacks to the library that caused me to look in another direction, and ultimately led me to creating Backbone.store.

My two greatest needs were:

  1. The ability to utilize the storage mechanism as a cache.
  2. A greater range of compatibility outside of localStorage support.

To meet these needs, I turned to Lawnchair, a JSON storage library with a wide range of adapters for different storage types, mixed in a little bit of the aforementioned backbone.localstorage plugin, and came out with the beginnings of a (hopefully) useful storage middleware.

While the overall project is in very early stages, its not too soon to look at, play with, and provide feedback on.

Download / clone from Github.

Features:

  1. Can be used with or without a backend server
  2. Configurable cache time to live
  3. Configure when to use server vs cached model – great for list  vs. detail of a record
  4. Does not require localStorage support, though a Lawnchair adapter may be needed to get the support you want.

Planned enhancements:

  1. Batch model upload to server (online/offline or time delayed)
  2. Parameter based caching (for caching paging / search results / etc)
  3. Remove Require (AMD) dependency
  4. ??? (accepting ideas)

Also included in the project is a basic ‘Todo’ style application including a CodeIgniter (php) based RESTful web server and structure, and a Backbone/Require/jQuery front-end consisting of a list/detail view with complete CRUD control and proper routing using both direct (bookmarkable) URL access and pushState routing.

Again, this project is nowhere near complete, so I’m sure there are some bugs… Still on the todo list is to add test cases using Sinon and Jasmine, and there is a known issue with the list -> detail -> list navigation.

 

 

jQuery DynoMenu – a plugin with a purpose

I’ve just added the first of hopefully many jQuery plugins and bits of useful javascript to Github.  Called DynoMenu, it is a plugin with a very specific and singular purpose.. yet it may have value to others who find themselves with a similar need.

DynoMenu was conceived to solve a problem on HomesConnect.  Since HomesConnect is a CMS that allows users to add new pages and new menu items to their navigation, and since the navigation is horizontal across the top of the page, the need arose to be able to maintain the layout of the templates while removing the burden of keep the navigation usable from the agent.

The solution was this plugin, which will first try to shrink the navigation font to a determined size, and if failing will systematically remove navigation items until it achieves the best fit. These items are placed in a ‘more’ link which becomes the last main navigation item, and can be accessed from there.  Since the agent can determine the order of their navigation via a drag and drop interface on the backend, it is easy for them to decide which items will fall under the ‘more’ link by pushing them to the end of the navigation.

In any case this may not have much use outside of CMS systems where you cannot control the total number of nav items, but who knows.. maybe there is a use case I’m not considering.

Download it from Github here.
See a demo.

A Quick Code Igniter and JQuery Ajax Tutorial

This tutorial assumes a basic working knowledge of Code Igniter. If you have never used CI before, please refer to the framework documentation

In the old days (2 years ago), working the Javascript magic to create a cool AJAX based event took a fairly decent working knowledge of the mechanisms behind the process.
Continue reading