I heard Ryan Bates say that the Rails JavaScript helper methods prevent "unobtrusive JavaScript".
Is this correct and, if so, could someone explain why?
I heard Ryan Bates say that the Rails JavaScript helper methods prevent "unobtrusive JavaScript".
Is this correct and, if so, could someone explain why?
Unobtrusive just means "Don't mix your HTML with your javascript behavior". Another way to say this is "Don't change your HTML just because you want to use javascript". The rails javascript helper methods do just that in a kind of hidden way.
Say you weren't using javascript at all. If you wanted an HTML form you'd use form_for
and have a regular form. Now say you want to add javascript so that your form submits an AJAX request instead of a regular HttpRequest. You have two ways to do this.
remote_form_for
The first method is obtrusive. You're changing your markup (look at the generated code). The second method is unobtrusive. By using jQuery and attaching the behavior from javascript you don't alter your HTML at all.
the rails helpers add the onclick events right into the html. i always thought unobtrusive meant that your page will still work when a user has javascript disabled, but after jason punyon's answer popped up while i was typing, i checked into it.
indeed the technique to completely separate javascript from the html has been labeled unobtrusive javascript.