Note that there are some explanatory texts on larger screens.

plurals
  1. POrails best practices where to place unobtrusive javascript
    text
    copied!<p>my rails applications (all 2.3.5) use a total mix of inline javascript, rjs, prototype and jquery. Let's call it learning or growing pains. Lately i have been more and more infatuated with unobtrusive javascript. It makes your html clean, in the same way css cleaned it up.</p> <p>But most examples i have seen are small examples, and they put all javascript(jquery) inside application.js</p> <p>Now i have a pretty big application, and i am thinking up ways to structure my js. I like somehow that my script is still close to the view, so i am thinking something like</p> <pre><code>orders.html.erb orders.js </code></pre> <p>where orders.js contains the unobtrusive javascript specific to that view. But maybe that's just me being too conservative :)</p> <p>I have read some posts by Yehuda Katz about this very problem <a href="http://yehudakatz.com/2007/05/17/jquery-on-rails-a-fresh-approach/" rel="noreferrer">here</a> and <a href="http://yehudakatz.com/2007/05/25/10/" rel="noreferrer">here</a>, where he tackles this problem. It will go through your js-files and only load those relevant to your view. But alas i can't find a current implementation.</p> <p>So my questions:</p> <ul> <li>how do you best structure your unobtrusive javascript; manage your code, how do you make sure that it is obvious from the html what something is supposed to do. I guess good class names go a long way :)</li> <li>how do you arrange your files, load them all in? just a few? do you use <code>content_for :script</code> or <code>javascript_include_tag</code> in your view to load the relevant scripts. Or ... ?</li> <li>do you write very generic functions (like a delete), with parameters (add extra attributes?), or do you write very specific functions (DRY?). I know in Rails 3 there is a standard set, and everything is unobtrusive there. But how to start in Rails 2.3.5?</li> </ul> <p>In short: what are the best practices for doing unobtrusive javascript in rails? :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload