I you have a highly interactive event driven page in rails 18.104.22.168 with Turbolinks.
Having different interactions on different pages, events like window.resize will trigger on pages where I don't really need it.
What is the rails way of doing this?
The whole point of the Rails Asset Pipeline is to gather your assets, minify them and concatenate them all into a single file that the browser downloads only once, the first time a user navigates to your page.
Yes, use jQuery to check if certain elements exist to run some behavior.
The way I've found useful is to not think about js behaviors as page specific but rather, widget specific. e.g. if there's a sortable table in the DOM then initialize the sortable table widget i.e.
The above example uses the jQuery plugins pattern. Place your behavior/widget logic inside the jQuery plugin and it will only run when the selector finds elements e.g.
something() will not be called.
somethingElse() will be called.
In the preceding examples, when an element is found and the function is called, the
this of the function references the matched elements and you can run your behavior logic on them.