CSS, live(), and application state

An idea I ended up thinking a lot about as a result of the application rewrite I just did was managing client-based application state with CSS classes. I’ve been making mental lists, trying to figure out how far you could take it and whether it would be useful. I was thinking there was probably some performance caveat or something, but then I read this post on the relative speed of live() and now I’m thinking maybe it’s something worth exploring more.

Using (what used to be) my application as an example, I can see a few possible states..

  • Edit mode for the object’s owner
  • Display mode for the object’s owner (so, surfacing links to access the edit mode)
  • Display mode for non-owners (no edit links)
  • Live mode (same as above, but possibly with more data about what the object is doing in whatever context it’s been published or released to)
  • Error mode (tracking needed corrections)
  • …?

You couldn’t use switching classes alone to manage those if there were roles certain information had to be hidden from – you’d have to take care of that on the server, obviously. And you’d have to check permissions on any type of update, but presumably you’d be doing that anyway.. For my application it would work fine, though. For certain pieces of the application, I implemented a pared-down version and it’s pretty successful.

What I actually tried was really simple. I just applied a CSS class to a container, causing pieces of the interface to be shown and hidden. Event handlers for those elements were attached with jQuery live(), so that as stuff gets added and removed, its event handlers are already (usually) set up and switching states merely reveals or hides the elements that trigger them.

Examples of WTF changing the “state”/container class actually does..

  • Show/hide edit buttons
  • Change an element’s event handler
  • Enable form elements
  • Change layout to reveal spaces where new elements can be added
  • Hide error messages
  • Disable iframes (I need them – don’t judge)
  • Determine whether areas that contain actions that aren’t automatically surfaced react to hovers
  • Reveal state-specific information

Especially with regard to changing event handlers, I’m assuming jQuery live() was designed with this use case in mind? What I mean is like:


$("div.editable a.editItem").live("click",function() {
showSomeEditor();
});
$("div.error a.editItem").live("click",function() {
alert("Nice try. Fix whatever you screwed up, then we'll talk.");
});

I think this is just handy as hell, but every time I try to google information about how other people might be doing the same thing, I come up with nothing. So in the absence of any best practices or warnings, I’m still playing around with it on my own. I think it’s cool.

Advertisement

5 Responses to “CSS, live(), and application state”


  1. 1 slexaxton June 16, 2010 at 6:27 pm

    Hey, I think everything you said you were guessing you were right on is pretty right.

    If you need backup – just reference Paul Irish’s AntiPattern for Performance of the Week in Episode 12 of yayQuery ( http://vimeo.com/9153395 ). (Starts at 21:30)

    Here’s the related jsfiddle example as well: http://jsfiddle.net/75JWK/

    Yay for coming to similar smart conclusions.

  2. 2 garann June 16, 2010 at 8:12 pm

    Nice! I’ll take a look at it when I can get near some decent speakers. I knew there had to be information about this stuff out there somewhere..

  3. 3 garann June 17, 2010 at 2:18 am

    Aha! (Just watched it.) Very cool, glad I’m not crazy. :)

    On an semi-related note.. You guys have an excellent fucking podcast. After a fairly crappy day and then watching that, I’m totally cheered up AND my roommate, who knows nothing about jQuery and was only half paying attention, also thought it was hilarious.

  4. 4 slexaxton June 18, 2010 at 4:37 am

    Thanks for the kind words. My girlfriend’s parents watch it, and they like it, so that’s the gauge I use for entertainment value.

    Also, it’s nice seeing good JS devs in town outside of the small group we normally have, so thanks for coming out, again. I look forward to seeing your presentation. :)

    Let us know if you want us to talk about anything on yayQuery too, or call the partyline (maybe at one of your work pizza parties?)…

  5. 5 garann June 19, 2010 at 1:29 am

    Ha, that’s awesome! Well, unless your girlfriend’s parents are Javascript developers. Then it’s still awesome but, you know, less surprising.

    Don’t have anything to present or ask your partyline.. Maybe if I keep paying attention I’ll come up with something. :)


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




RSS Recent posts on the live version of this blog

  • is it me or are we going backward? February 23, 2012
    Maybe it’s just me, but it feels like lately diversity is falling out of fashion as a goal in this industry. I made a tweet seeking sponsors for All-Girl Hack Night’s SXSW event earlier and was surprised by some of the responses. It seems like recent conferences have fewer female speakers, or are more defensive [...]
  • the $150k solution December 11, 2011
    An article was published yesterday in one of Austin’s local papers about Austin’s tech talent shortage. I was job hunting just a couple months ago and get a lot of calls from recruiters and hear about friends’ companies who are hiring and I think it’s pretty damned accurate. And by accurate I mean that it [...]
  • “girl” power November 16, 2011
    I just watched a short video called How to Get More Women in Tech in Under a Minute. The speaker, Caroline Drucker, is making a point about the toxicity of the word “girl” and how it hurts the cause of women in technology. Her argument is that every time we refer to ourselves as girls, [...]
  • calling the github API with node.js September 4, 2011
    Updated: when I originally posted this, I wasn’t able to connect to the github v3 API. That’s fixed now, and several pieces of code are different as a result. Namely: removed http.createClient() – that’s deprecated, oops require the https module save the oauth token in the session completely change the way the API gets called [...]
  • widgets for third-party sites July 4, 2011
    For the past couple weeks or so I’ve been driving myself crazy researching the ways people are developing widgets to run on other people’s sites. I’ve been writing and rewriting the same piece of JavaScript on the daily as I find new information or start to doubt the information I’d decided previously was The Right [...]
  • no country for old hackers July 2, 2011
    This was actually a presentation that I was hoping to submit to a conference I really wanted to speak at. The reason I didn’t ultimately submit anything is the same as the topic of the presentation/post, in part. How’s that for meta? And since I’ve been thinking of it as a presentation, I’m just going [...]
  • separation of concerns is a bunch of bull June 25, 2011
    A couple days ago I was booking myself some air travel. I finished filling out all the forms and submitted my payment info and finally it dropped me on the confirmation page. The confirmation itself was tiny, and the rest of the page was taken up by hotel offers, several of which were stupid expensive [...]
  • nested callbacks in jquery begone!! June 2, 2011
    A week or two ago I noticed a bunch of people having trouble because of their nested callbacks, or with flow control in general, in jQuery. I said something about this on twitter, saying people should use jQuery’s Deferred object, and several people commented that Deferred is a really difficult concept to explain. I kind [...]
  • things that actually matter May 8, 2011
    I had just gotten some very relevant facts about this and was about to update with corrections, when Alex Sexton posted his comment below. Please scroll down and read that before taking this without a giant grain of salt. I definitely jumped to some incorrect conclusions, and he’s got the real story down in the [...]
  • keyboards April 4, 2011
    When I was in college, I worked in the computer lab. One of the very first things they told us during training is perhaps the best thing I’ve ever learned in my entire career working with computers: never touch someone else’s keyboard. It doesn’t matter if the keyboard involved is actually yours, and someone else [...]

Follow

Get every new post delivered to your Inbox.