views:

30

answers:

1

I've been programming rails for only a few months and was first introduced to RJS in 2.3.8 rails. Though things have changed slightly and am a little confused with this "unobtrusive javascript". From reading google, I'm understanding it as like removing the javascript inline to a seperate file.

From what I understood of .rjs was it was already doing this.

Can someone explain the difference, if there is one? I'm still trying to make that leap over to unobtrusive JS cause it seems like it's the rails 3 way but I'm having trouble making that jump. Or is unobtrusive javascript "pure" js and not like the ruby way as I've understood that .rjs is a wrapper for javascript. Should I take the clue that I need to start learning java script? I know very little of javascript.

Thanks in advance for any advice/tips.

+1  A: 

Unobtrusive means not doing things inline, as you said. RJS is something different, basically ruby helpers that generate javascript. What unobtrusive means for rails is that instead of generating js inline, it will instead decorate the dom objects with information enough to figure out what to do, and then use event delegation as an alternative technique to just dumping a script tag into the middle of the page.

This results in much cleaner output, and potentially better performance, both client side (don't have to re-run javascript all the time if you are using event delegation), and ajax-wise (not doing things inline means you don't need to push javascript over the wire as well as your html).

RJS is a whole different thing, and was pretty much un changed by that move. Typically, I find that people who aren't that familiar with how javascript works prefer the rjs approach, since it just does the job. If you are really serious about your js, chances are you will either be using js.erb files, or just doing returning json from the server and dealing with everything else fully client side.

Matt Briggs
Thank you for your response. Not sure if you can answer this or not, but so if you do not want to use unobtrusive js in rails, then you would not use the :remote=>true argument then instead just use the normal link_to_remote?
RoR
all the "remote" helpers are deprecated, in favor of the new :remote => true syntax. Like I said before, the new ujs stuff and the RJS helpers are really two different things, so using rjs doesn't mean you can't use the ujs helpers
Matt Briggs