How we use AJAX in the all-new www.vark.com

Using Aardvark is a highly interactive experience — it’s about actively engaging in conversations with other people, not about passive consumption of content.  Using our IM, email, Twitter and iPhone interfaces, it’s easy to jump right in and start having conversations.

When we designed the new Aardvark website (launched 2 weeks ago!) we wanted to create an interface that was similarly engaging by being very interactive and highly responsive.  This post is about some of the technology we built to power the new web interface.  (This is more technical than most posts on the Aardvark blog, so apologies to all of the non-nerd readers out there…)

JSONH

Vark.com is a Ruby on Rails application that makes heavy use of client-side JavaScript (JS).  We wanted our Rails app to behave like a web service that only serves data and the JS to behave like a client that consumes data — instead of generating JavaScript server-side.

JSON is the obvious format to use when passing data between Rails and JS, but in Rails, the :json format is conventionally used to serve ActiveResource compatible output.   So we decided to use a new format and named it :jsonh (after “json+html”).

The :jsonh format is a regular JSON object with a consistent interface that exposes 3 fields: ’status’ (with the HTTP status code), ‘flash’ (with Rails flash messages) and ‘html’ (optional - with html needed for update on the client side).

For example, a typical response on a validation error could be:

{
 status : "422 Unprocessable Entity",
 flash : { errors:["Login/Password are invalid"] },
 html : "<form>....</form>",
}

varkify!

In the client-side JS we make use of the jQuery library.  But even with jQuery, implementing AJAX functionality tends to be repetitive — so we built functionality in between Ruby on Rails and jQuery to help us automate some of the AJAX patterns we use.  We call this set of functionality “VARKificiation”.

Here’s the core pattern we automate using VARKificiation: We have a form or a link and when a user clicks it, instead of reloading the page, we want the client-side JS to submit an AJAX request. Then based on the result of that request JS can decide to update some of the DOM, display a message, clear a form, etc…

Using VARKification, we can simply “varkify” a form.  varkify() will bind the “submit” or “click” event (depending if we apply it to a link or a form) — then when that “submit” or “click” event is triggered it will figure out the url, method and parameters to submit to the Rails app by looking at the DOM.  Once the request is complete it will run the callback passed.  The response is passed as a parameter:

$('form#new_session').varkify(function(json) {
  // when the request is complete
  ...
});

Putting it all together

So now, using :jsonh and VARKification, we can write this code:

$('form#new_session').varkify(function(json) {
 // when the request is complete
 if (json.success) {
   // if we received a 2xx status code, redirect
   window.location.href = json.location;
 } else {
   // if there was a validation error, reload the form
   $(this).html(json.html);
 };
});

varkify() will display the flash errors automatically and the callback lets us update the page according to the status code and html from the :jsonh response.

Since we isolate our JavaScript from our HTML code, when we update a piece of DOM that new code has no behaviors associated with it and we need to rebind them.  All we need to do is call the following helper in our view:

<% add_behaviors 'VARK.behaviors.signin_form()' %>

This will add one more element to our jsonh response, so behaviors can be loaded by varkify() with some eval() magic:

{
  status : "422 Unprocessable Entity",
  flash : { errors:["Login/Password are invalid"] },
  html : "<form>....</form>",
  behaviors: 'VARK.apply_behaviors(["VARK.behaviors.signin_form()"])'
}

Other patterns

In creating vark.com we codified many more patterns, and took advantage of the best that Rails and jQuery had to offer.  For instance, within the VARKify framework we can do things like prevent double-submits on forms, standardize the appearance of spinners in the UI, and lots more.

And now for the plug ;-) … If you’re interested in front-end engineering — or, for that matter, systems engineering, cloud computing, machine learning, or search algorithms — you should consider working with us to solve interesting problems.

6 Comments

  1. Posted October 30, 2009 at 7:42 pm | Permalink

    Interesting, I was just doing something like this for a project I’m working on. Doing AJAX form submits with status updates, with jQuery no less. In my case, I was using Groovy/Grails on the backend, but I nonetheless would love to ask — are you guys planning on open sourcing this in any way?

  2. Posted November 2, 2009 at 10:58 am | Permalink

    1) It sounds like you’re duplicating some functionality from the jQuery form plugin (http://jquery.malsup.com/form/)

    2) Instead of actively re-binding events to new DOM elements you should use jQuery’s live() function (or something similar — the point is that you want to use event delegation rather than explicit event binding)

  3. Posted November 2, 2009 at 12:38 pm | Permalink

    Andrew, we’d love to open source it. We just need to find the time to package the code and make it more generic for reuse. I’ll try to spend some time on it.

    Tom, thanks for the comment. Indeed we’re doing a lot that the jQuery form plugin does already. With the level of control that we want, we had to go and write our own code. As for event delegation, I’m all for it and we take advantage of it in some places. What I’ve come to notice though is that it does not apply very often since so few event actually bubble up.

  4. Posted June 9, 2010 at 1:02 am | Permalink

    1) It sounds like you’re duplicating some functionality from the jQuery form plugin (http://jquery.malsup.com/form/)

  5. Posted June 9, 2010 at 1:02 am | Permalink

    I’ll try to spend some time on it.

  6. Posted July 10, 2010 at 1:35 am | Permalink

    This is so useful, thank you!

2 Trackbacks

  1. By Ajaxian » A State of the Web via October Tweets on November 2, 2009 at 4:44 pm

    [...] of Aardvark has written up a short post on how they use JS on the new vark.com 4:17 PM Oct [...]

  2. By A State of the Web via October Tweets on November 4, 2009 at 7:19 am

    [...] of Aardvark has written up a short post on how they use JS on the new vark.com 4:17 PM Oct [...]

Post a Comment

Your email is never published nor shared. 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>