Introduction
For my new job I have to write a classical dropdown menu example that is highly accessible (by mouse and keyboard) and that is compatible to screen readers (by managing WAI-ARIA attributes and states properly). My team consists of "normal" people and people with bodily disabilities, and they say they weren't able to find a fully accessible dropdown menu until this point, so we are trying to create our own to give to clients.
Well, I'm coming from a Ruby on Rails background and didn't write complex JavaScript code until now, so I'm trying to apply my OOP knowledge from Ruby to the JS world. Since I know CoffeeScript quite well, and it's offering quite some magic in the background which makes working with classes and inheritance easy (although JS is an object driven language and there's no such thing as classes), I'm sticking to CoffeeScript instead of plain JS.
I'm trying to set up some proper object tree that resembles a dropdown menu:
- There's an
Item
class. - There's a
ParentItem
class which inherits fromItem
. - There's a
RootParentItem
class which inherits fromParentItem
.
This way, all root items are of type RootParentItem
, all other parent items (items that hold their own sub menus) are of type ParentItem
, and all the "real" menu items (links pointing to some place in the website) are of type Item
.
By setting up this proper object tree, I think it's very simple and reasonable to handle different key actions, mouse actions, etc., depending on the type of item. This helps to prevent long procedural spaghetti code, that I have seen on many other JS libraries which are providing similar functionality.
Here's an example of my current work: http://codepen.io/jmuheim/pen/fAjcx
Now after some thinking I asked me the question: is it reasonable to create my own object tree, while having a lot of jQuery arrays in the background and keeping them in my objects' variables? It somehow feels odd to have a nice tree structure, but always working with a "shadow" of quite the same structure, say: jQuery arrays.
As mentioned, I'm coming from a Ruby world, which is highly object oriented. I don't have any experience in clean coding practices/patterns in JavaScript world, so:
- Do you think my attempt is reasonable?
- Is there a cleaner (publicly more accepted) way of doing this? Maybe working directly on the prototypes of the jQuery arrays would be more reasonable? How could this be done though?
- Any other comment about my JS coding style?