简体   繁体   中英

Rails asset pipeline - custom js files

I have couple of js files in application.js file. Such as;

//= require jquery
//= require jquery_ujs
//= require dropzone
//= require jquery.cookie
//= require toastr

//VENDOR JS BEGINS
//= require pace/pace.min

//= require modernizr.custom
//= require jquery-ui/jquery-ui.min
//= require boostrapv3/js/bootstrap.min
//= require jquery/jquery-easy
//= require jquery-unveil/jquery.unveil.min
//= require jquery-bez/jquery.bez.min
//= require jquery-ios-list/jquery.ioslist.min
//= require jquery-actual/jquery.actual.min
//= require jquery-scrollbar/jquery.scrollbar.min
//= require bootstrap-select2/select2.min
//= require switchery/js/switchery.min
//= require imagesloaded/imagesloaded.pkgd.min
//= require jquery-isotope/isotope.pkgd.min
//= require classie/classie
//= require codrops-stepsform/js/stepsForm
//= require bootstrap-datepicker/js/bootstrap-datepicker
//= require bootstrap-datepicker/js/locales/bootstrap-datepicker.tr.js
//= require bootstrap-datepicker/js/locales/bootstrap-datepicker.en.js
//= require summernote/js/summernote.min
//= require moment/moment-with-locales.min
//= require bootstrap-daterangepicker/daterangepicker
//= require bootstrap-timepicker/bootstrap-timepicker.min
//= require codrops-dialogFx/dialogFx
//= require ion-slider/ion.rangeSlider.min
//= require owl-carousel/owl.carousel.min

I have also couple of js codes in the html.erb pages. But I want to take all page specific js codes into 1 file. This file should be called after page loads. Because some of them uses ruby code such as init lat lng of google maps. Also, couple of js files such as;

    //= require codrops-dialogFx/dialogFx
   //= require ion-slider/ion.rangeSlider.min 

works after page loads. So, they do not work as I put them to head tag as;

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>    

So If I, design application.js as;

//= require jquery
//= require jquery_ujs
//= require dropzone
//= require jquery.cookie
//= require toastr

Then call <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> in head tag then call all the other js files in the body tag like

<%= javascript_include_tag 'pace/pace.min', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'modernizr.custom', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'jquery-ui/jquery-ui.min', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'boostrapv3/js/bootstrap.min', 'data-turbolinks-track' => true %>
....

Does rails precompiles these files also?. I have these js files in vendor file.

Yes Rails will precompile all files it finds in a javascript_include_tag assuming it can find them within your asset path.

However, it's redundant to add javascript_include_tag for assets which are already declared in your application.js as they'll actually get loaded twice in your HTML.

If you need to include assets outside of the normal load path, you'll need to tell Rails to precompile your custom files in your application.rb:

config.assets.precompile << 'path/to/asset'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM