(code and slides)
Dan Webb’s Unobtrusive Ajax With Rails talk proved so popular yesterday that it is being redone in the main hall during lunch today.
Unobtrusive Ajax (or JavaScript really) is all about removing all those inline onclick=”return myFunc(an_id)” bits of code from your HTML and into your JS. Separating behaviour and mark-up. Attaching behaviour to your class definitions e.g. class=”productbutton”.
At the very least this makes your pages smaller (think of a 1000 item list which has onclicks inline vs. attached via a class in a separate JS.)
At the most it is the idea of the class assigned to HTML elements.
A big theme is graceful degredation for when JS is not available.
A good point is around a href=”#” which semantically and functionally doesn’t make sense. I am an offender here but aim to change.
button_to is a little known Rails helper that generates a button in a form. This is for when you should be using buttons and not links e.g. for delete links. Remember, links should not have side effects (if for no other practical reason than avoiding pre-fetch technologies like the Google Toolbar that will wipe your records.)
To get started with UJS checkout the Rails plugin at ujs4rails.com.
A good talk and as Dan says, it isn’t rocket science. It has obvious benefits even if you don’t care about semantics. Hopefully Rails takes the ideas into the trunk.
