views:

50

answers:

2

A phenomena I'm seeing more and more of is Javascript code that is tied to a particular element on a particular page, rather than being tied to kinds of elements or UI patterns.

For example, say we had a couple of animated menus on a page:

<ul id="top-navigation">
    ...
</ul>

<!-- ... -->

<ul id="product-list">
    ...
</ul>

These two menus might exist on the same page or on different pages, and some pages mightn't have any menus.

I'll often see Javascript code like this (for these examples, I'm using jQuery):

$(document).ready(function() {
    $('ul#top-navigation').dropdownMenu();
    $('ul#product-selector').dropdownMenu();
});

Notice the problem?

The Javascript is tightly coupled to particular instances of a UI pattern rather than the UI pattern itself.

Now wouldn't it be so much simpler (and cleaner) to do this instead? -

$(document).ready(function() {
    $('ul.dropdown-menu').dropdownMenu();
});

Then we can put the 'dropdown-menu' class on our lists like so:

<ul id="top-navigation" class="dropdown-menu">
    ...
</ul>

<!-- ... -->

<ul id="product-list" class="dropdown-menu">
    ...
</ul>

This way of doing things would have the following benefits:

  • Simpler Javascript - we only need to attach once to the class.
  • We avoid looking for specific instances that mightn't exist on a given page.
  • If we remove an element, we don't need to hunt through the Javascript to find the attach code for that element.

I believe techniques similar to this were pioneered by certain articles on alistapart.com.

I'm amazed these simple techniques still haven't gained widespread adoption, and I still see 'best-practice' code-samples and Javascript frameworks referring directly to UI instances rather than UI patterns.

Is there any reason for this? Is there some big disadvantage to the technique I just described that I'm unaware of?

+1  A: 

First of all I agree with you that using the class approach is better, in general.

But I don't think I'd go so far as to say it's less coupling of the code to the UI. If you think about it, if the code assumes ID "foo" vs. class name "foo", you still have to know that when working with the UI. There's still a 'contract' between them -- whether you meet it through ID or class is not really different.

One disadvantage to using the class approach I'd imagine is speed -- it should be faster to find a particular element by ID than find potentially multiple elements by class. The difference is probably completely negligible though.

But, in the case where your code is designed to attach multiple behaviors, as in your two-dropdown example, using class certainly makes more sense. That is less coupling since your code is a bit more generalized, and your UI more likely to be customizable w/o changing the code.

One thing I'd change in both of your examples... why have the UL in the selector? If the code knows it can only possibly work if the target is a UL, well, that's one thing -- but in that case, it'd be better to avoid the UL in the selector and let the code throw a meaningful error if the target is found not to be a UL, lest the page just do nothing without any indication as to why (e.g. because the UI put the ID/class on a OL).

So in other words, just "#foo" or ".foo" not "ul.foo", etc.

I should point out that in case someone thinks the UL somehow makes the selector more efficient, it doesn't, since selectors are evaluated from right to left.

InfinitiesLoop
Using classes certainly doesn't remove the necessity for the developer to consciously develop code in a loosely-coupled way. Actually, it's probably better to use IDs in cases where a particular element will only occur once per page.I guess I'm not advocating classes as such, but a way of writing Javascript so that it works with *patterns* and *micro-formats* rather than each individual, unique use-case of a particular patten.
jonathanconway
The UL selector is there because it seems reasonable to assume (and enforce) that menus should be structured lists. This fits in with the best-practice of *semantic HTML* - of using appropriate HTML elements to represent structured data, rather than doing everything with Divs and Spans.
jonathanconway
As far as performance/efficiency, you may very well be right about classes not being fast. There are ways to alleviate this, but they would come at the expense of clean HTML/JS separation. I guess one way to approach it is to have a separate piece of JS just for IE6 (the only browser that gives any real headaches), which directly binds to IDs rather than classes.
jonathanconway
re UL: Sure, nothing wrong with that. My point though was what would happen if the UI tried to use a non-UL. Nothing would happen since the selector wouldn't find it. If the plugin only works with ULs, better would be for it to check the tag name and throw an error.
InfinitiesLoop
+1  A: 

Your approach is preferred.

The reason people do things in different ways is because they can and it still works.