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.

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

  • giving greenfield stuff to newbs May 23, 2012
    I’m coming up on two months at my new job (feels like a lot less), and with the amount of code we have, that means there’s still plenty I haven’t seen or worked with. There are certain processes (e.g., deploying) that have become automatic, but others I haven’t yet done. I still get automated emails [...]
  • javascript hotline May 17, 2012
    I don’t know Rails, and that may be why I hadn’t heard of Rails Hotline before last week. If you’re not a Rails dev and are also unfamiliar, it’s pretty much what it sounds like: developers volunteer to take a shift answering the phone if anyone calls, and other developers can call in and ask [...]
  • authority and paying your dues May 11, 2012
    I’ve tried consulting a couple times. The most recent of those, I tried it with a partner. He didn’t know much about web development. I had to teach him a lot. He got significantly better, but he was still what I’d consider junior. At some point after we gave up on consulting, he was applying [...]
  • from mockup to code (efficiently) May 1, 2012
    Lately I find myself in the once-familiar position of taking mockups and converting them to markup and CSS. This is generally a pretty simple task, one you could optionally pass off to a junior dev. But that assumes there’s a clear process for it. Obvious divisions of labor, patterns, and best practices, I’m finding, make [...]
  • girl ghettos April 13, 2012
    The double-edged sword of trying to address the lack of women in technology as a woman is that, if you manage to get a group of technical women together, you’re going to end up creating what’s called a girl ghetto. A girl ghetto is where you stick the women you’re keeping around to show you’re [...]
  • a retirement fund for your javascript April 9, 2012
    You ever find yourself feeling unreasonably protective of JavaScript? I do. For a long time, JS lacked a lot of sophistication. It was vulnerable to being trampled over by bigger, more powerful technologies like Java applets or Flash. There was nothing to really commend it to anyone who didn’t, for whatever crazy reason, already love [...]
  • choosing template engines March 3, 2012
    The first presentation I ever gave at a conference was on templates. jQuery templates, specifically. It’s weird – it’s a topic I’ve never presented about again yet one that, if anything, I’ve become more obsessed with. So it stands to reason then that I still find myself in conversations about templates with other JavaScript dev […]
  • 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, [...]

Follow

Get every new post delivered to your Inbox.