Why do Rails helper methods prevent "unobtrusive JavaScript"?

I heard Ryan Bates say that Rails JavaScript helper methods prevent "unobtrusive JavaScript".

Is this correct and if so can someone please explain why?

+2


source to share


2 answers


Unobtrusive simply means "Don't mix your HTML with your JavaScript behavior." Another way to say is "Don't change your HTML just because you want to use javascript." The javascript helper methods in rails do this as something hidden.

Say you haven't used javascript at all. If you want an HTML form, you can use form_for

and have a regular form. Now tell me that you want to add javascript so that your form will submit an AJAX request instead of the normal HttpRequest. You have two ways to do this.



  • You can use helper method remote_form_for

  • You can use something like jQuery to bind a function to a submit call that submits your form via AJAX.

The first method is intrusive. You change your markup (look at the generated code). The second method is unobtrusive. By using jQuery and adding behavior from javascript you are not modifying your HTML at all.

+3


source


rails helpers add onclick events directly to the html. I always thought unobtrusive means your page will work when the user has javascript disabled, but after jason punyon's answer came up as I typed, I checked it.



indeed, the method to completely separate javascript from html has been flagged with unobtrusive javascript.

0


source







All Articles