jQuery: binding DOM events to objects

A few days ago I was working on a project for a client that basically involved allowing a non-technical end user to build arbitrarily complex boolean queries using a UI. The user could basically click, configure, and drag/drop queries to build expressions like (A AND B OR C) AND (Z OR X). They would also need the ability to edit the pieces in-line, toggle ANDs to ORs, and so on.

Anyway, not to difficult to represent with a data structure but the complexity was going to be in tying the UI to the data structure with callbacks and events. Usually, I would of used a single $(“a”).click() handler to handle all the edit, delete, and configuration clicks in the UI but that quickly devolves into a disastrous mess of if statements and hasClass() checks.

Hoping to avoid this, I started thinking about cleaner ways to implement this and realized if I actually bound events to the individual objects they were going to effect the resulting code would be much cleaner. The UI elements were going to be dynamically generated each time the data structure was updated anyway so binding $.click() events on the <a> tags to their corresponding objects wouldn’t be to much extra work.

All in all, things worked out pretty well. The final code is much easier to follow and there isn’t a gigantic if() block which is impossible to trace.

I can’t share the actual implementation I used but I threw together an example which outlines the technique. Check out the JSFiddle at http://jsfiddle.net/fN46q/4/

Looking at the code, the Board object is an Array that in turn contains Piece objects to form a 3×3 grid.

The Piece objects individually supply a render function to display themselves and then contain a click() function which handles their $.click() events.

The key line which makes this work is:

   $(td).find("a").bind("click", {piece: this[i][j]}, this[i][j].click);

What the second parameter does is add the object into the Event object that is passed to the event handler. More info is available on the $.bind() documentation but looking at the click() function in the Piece object you can see where it comes in to play:

 click: function( e ){
    e.data.piece.val = !e.data.piece.val;
    e.data.piece.board.render();
    return false;
 }

So effectively, the data structure is now linked to the DOM.

As always, thoughts, comments and are feedback welcome!

Congrats: 500pearlstreet.com launched!

Our friend Walt launched 500Pearlstreet.com earlier this week. It’s a blog that will be focused on covering white collar crime in the news as well as exploring other topics related to white collar crime. We’re excited to see where Walt takes the blog and wish him the best of luck!

Technically, 500Pearlstreet.com is running Drupal 7 with a couple of of pretty neat modules:

  • The theme is a standard sub-theme of the excellent Omega responsive theme. The amount of configuration possible through the Omega UI is really impressive and a welcome change from how difficult theming was in Drupal 6.
  • In-context links and images are being automatically powered by Zemanta which is pretty neat.
  • OpenCalais is also running in the background providing semantic tagging capabilities. These aren’t exposed yet but hopefully will become useful when there is more content.

Anyway, it should be a good read so drop it into your readers!

The Setfive coffee table!

A couple of months ago I stopped into DangerAwesome! and was instantly intrigued. DangerAwesome! has a bunch of laser beds and which in turn let you laser etch pretty much anything.

I started kicking around the house for something to etch and obviously decided on our toilet seat.

Here’s how the toilet seat came out:



Anyway, after seeing how well the toilet seat came out I decided to try something a bit bigger. After a quick trip to Jordan’s Furniture I ended up with an awesome glass coffee table that obviously needed to get etched.

After some serious debate, I finally decided on etching a maze and a Setfive logo onto the table.

Here’s how it came out:



And then a close up off the {5}:



Pretty sweet. If you find yourself in Central Square definitely stop in to DangerAwesome! and give the lasers a shot.

tru.ly releases first free age verification service

Last week was a big week over at tru.ly!

We launched our lightweight social verification service that allows partner sites to verify that their visitors are 18+ or 21+. We’re hoping that this will replace current age solutions and allow sites to safely show and monetize 18+ and 21+ content.

Check out the awesome write up The Next Web did (Thanks Courtney!).

Anyway, theres a live demo at https://tru.ly/social-api-demo/ and the demo video is below.