I am using different javascript libraries in different part of my rails app. Currently all my library files are in application.js manifest file. And it gets loaded in pages, where it's not required. Is there any way I can load files that are needed in a particular part of my app?
-
You probably want to create a separate layout file.Antarr Byrd– Antarr Byrd2016-07-11 16:18:27 +00:00Commented Jul 11, 2016 at 16:18
-
Is that the only way?user6575502– user65755022016-07-11 16:19:31 +00:00Commented Jul 11, 2016 at 16:19
-
I read require js can be used. Will it solve my purpose?user6575502– user65755022016-07-11 16:20:05 +00:00Commented Jul 11, 2016 at 16:20
Add a comment
|
1 Answer
You can add a separate "manifest" and load that on demand where you need it. You will have to:
1- Tell Rails to preprocess those files, ie if your file is named additional_dependencies.js, then add it in assets.rb:
Rails.application.config.assets.precompile += %w( additional_dependencies.js )
2- Load that file where you need it (NOT in the main layout):
= javascript_include_tag 'additional_dependencies'
And, of course, do not add the files you want to keep as separate dependencies into the application.js file (watch for require_tree).