Arif Arif - 3 months ago 6
jQuery Question

Wrap a single event around code from multiple JS files? [Rails]

Is it possible to wrap a single

turbolinks:load
event around all my
js
files? So I don't have to specify
document.addEventListener("turbolinks:load", function()
on every
js
file that I create? Also, on a side note, can having your JS code in multiple files as opposed to having all in one file have an adverse effect on performance (although they're going to be compiled to a single file in production anyway)?

Answer

Is it possible to wrap a single turbolinks:load event around all my js files?

Yes. Just add your code (document.addEventListener("turbolinks:load", function()) in another .js file and include it before other scripts.

Or, if you're using a bundler as you say, just import or require the file from the other files. Or, just add it to the main html file.

can having your JS code in multiple files as opposed to having all in one file have an adverse effect on performance

No. Because, as you say, you're bundling it.

Comments