Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I too ran into this issue. In a large project you can have somebody put code into document ready to, for example, add a click function to each <strong>li</strong> within a <strong>div</strong> with class <strong>container</strong>.</p> <p>Now we could all argue that the above code would be too generic and of course may affect <strong>li</strong> tags in other parts of the application, but the bigger the project, the more likely it is that you will run into a conflict like this leading to unexpected behaviour.</p> <p>I for one am uncomfortable with a whole bunch of document ready functions running for each and every page loaded. My solution is not necessarily the perfect one, but it's one that I have taken up and will share with you. </p> <p>In the body tag of each page I add data elements signifying the controller and the action. I then have one document ready script that looks for a class named after the controller with the name Ready appended e.g. HomeReady. It will then call a method on this class (presuming it exists) named after the action. So in your asset coffee file you could write:</p> <pre><code>class @HomeReady @index: -&gt; alert("Hello") @show: -&gt; alert("Goodbye") </code></pre> <p>This allows control right down to the action level. When I came across your question I decided to package this solution into a gem as I have already used it in several projects. You can find it at: <a href="https://github.com/intrica/rails_document_ready" rel="nofollow">https://github.com/intrica/rails_document_ready</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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