Monday, May 25, 2015

Rails 4: Page Specific JS Load

To load JS files only on particular page:


1. Stop loading it globally
    In application.js file, stub (stop) loading the js file, you don't want to load.
 
    Example:
     //stub file_name


2. Add to Precompile
     Update config/initializer/assets.rb with following line to enable assets precompile.
      Rails.application.config.assets.precompile += %w( file_name.js )

     Rails throws 'AssetFilteredError', if the file is not precompiled.


3. Load JS file in specific folder
    Load the application JS and specific file JS where needed.
    Example:
      = javascript_include_tag 'application''data-turbolinks-track' => true
   = javascript_include_tag 'file_name''data-turbolinks-track' => true 




To call document ready for specific page:


1. Introduce new class to body or div of the page
2. Use jQuery selector for that class

Example:
  %div{"class": "_class_name"}


:javascript
   $("._class_name").on("ready", ready_function);
  

No comments:

Post a Comment