0

I am starting to understand the asset pipeline in rails 3.1 and am now using the jquery-rails gem to its full potential i.e. using

 // require jquery_ujs

and not putting the relevant jquery library in my assets/javascripts.js directory ( Rookie mistake i know)

My next question is relating to where i out my jquery/javascript scripts. At present i am placing them all in my application.js file. Would it be better to put the scripts on the relevant pages that they relate to. e.g. at the moment i have a calendar page that uses fullcalendar.js, should i be putting the script on this page under

 <script type="text/javascript"> .........</script>

at the bottom of the page, or do i keep it in the application.js file.

I have also noticed that the rendering of my pages that require jquery seem to be loading faster, is this because i am now no longer calling them twice?

All advice appreciated

Thanks

Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

1

Putting whole scripts at the bottom of specific pages is not a good idea. Using

<script>page_a_specific_function();</script>

at the bottom of pages to initiate page specific javascript is a better solution.

You could also assign specific ids or classes to some elements on specific pages and then check if those are present with javascript. If your page has an element with id="calendar", then specific part of your .js file would run.

As for where to place your javascript, you could stick to usual locations (which can be referenced in application.js and nicely compiled into one file). I put all my application specific javascript in app/assets/javascripts, and third party libraries in vendor/assets/javascripts. Third location is lib/assets/javascript.

Similar question on SO
asciicast about asset pipeline

Community
  • 1
  • 1
afaf12
  • 5,163
  • 9
  • 35
  • 58