It’s short, sweet, and unbelievably horrible! I knew there had to be a better way, but JS was this abstract beast. Now, having learned just a tad of the language, below is the new incarnation. (and the jsFiddle)
It’s still not complete (styling, disabling/enabling of buttons when appropriate, etc) but it’s a huge step in the right direction. You might think it’s complete overkill for the desired effect, but there are benefits:
- Seperation of concerns. The view handles the html, the timer handles itself, and the controller manages all of them
- Single Responsibility Principle – each class does one thing and one thing only (controller being the slight exception)
- You can easily tell what’s going on by looking at each class
- Testable. Although there are no tests at this time, it can easily be tested since each section can be tested in isolation
When you first look at it, it just works. You don’t really know how, but since you don’t really understand it, you just take it for what it is. Once you start to understand it, you hit that ‘oooooh, wow. That’s pretty cool.’ That’s what jQuery and the like is for the vast majority of people. It’s a magic box that just works. I’m slowly starting to open that box… and there’s some crazy ish in there.
If you are attempting to bind to an element from within a method inside a class that calls another method inside that class, you will have to pull some JS ninja-ry.
If you attempt to call StartTimer(), when you click the element with elementId, it will throw a reference error when it attempts to call sayHello(); In this context, sayHello does not exist. The reason is, this is the element that the event is bound to. To get around this, we have to get a reference to the method from outside the event listener so that it will be in the parent scope of the method and thus accessible from within the listener. Therefore the code above needs to be:
Hopefully this will save someone the time and hackery of trying to tie things to the DOM and not using JS in a more appropriate way!
Yup, I’m done. I’m tired of simply learning what I needed to get by with, or using the random jQuery plugin I found when solving a problem.
So why do most people simply hack JS? I think it’s because we were around when the only thing you could do was alert(). That was great and all, but we fell behind and didn’t learn the ‘scripting’ language because it wasn’t important. It’s why JS has been thrown into utility folders and never organized – one reason the Rails team moved towards organizing JS in projects, because they saw themselves doing it too.
JS is not a second class language anymore. It’s probably the most universal – it’s on every browser and it is, in fact, quite powerful. jQuery, Node, Backbone, Angular, etc, etc, etc.
With CSS3, we can now have better hover effects that the usual :hover we’ve used for years on links. Although, getting the timing correctly on the hover over and hover out can change the way the application feels.
- If the hover on is too slow, it can create a sense of anxiety in the user
- If the hover on effect is too slow, it can make the site appear to be slow as well
- Using different timing functions changes the overall feel. With a faster animation time, ‘ease-in’ make it appear as if there’s a delay on the hover effect because of the curve of how the animation timing is executed
- Using a longer hover out effect creates the feeling that the app is alive and kicking
- Too long of a hover out effect and it can be confusing as to where the user was just hovering
- Too long of a hover out effect just looks like it’s for show
- Hover on and out effects when coupled create a ‘flash’ feel
- Hover on without hover off creates an abrupt definition to the user interaction
- Font, size and location all matter when setting the transitions. Headers links and normal links can behave differently to carry the same application feel.
Ideally you want to aim for transitions that are not noticed. It’s the same principle when adding drop shadows to elements: if you notice them, then they’re too strong. Just like transitions, you want them subtle – make user interactivity appear as one fluid motion.
Here’s a fiddle with examples.