Reputation: 5245
I have these a coffeescript file for a model entree
that just instanciates a class in another coffeescript file:
jQuery(document).ready ->
ch = new CepageHandling
ch.handleKeyPress()
The handlePress
function captures keyup
events on a control.
I have another model vin
where I want to enable the same feature. I'm surprised to see I don't need to do anything (it already has the same html), it's already working, even though the coffeescript for the vin
model is completely empty. I assume that the created javascript for entree
gets called even when I'm not on this page.
I have seen the same behavior with scss
files, where style defined for one model gets applied to others if the descriptors match. Can someone explain (or point to some article) if this is normal behavior that assets are not isolated in rails? I really have a hard time grasping how it works.
Upvotes: 1
Views: 52
Reputation: 15089
It happens because of the manifest file application.js, more precisely in this line of code:
//= require_tree .
What this means is that all .js files contained inside the /assets/javascripts/ folder would be loaded to your views.
As your entree.js is already loaded and using the JQuery document ready function, it searches for the rules of your file inside all of your views.
This means that all of your pages that contains this ch element will get the same behavior.
Upvotes: 1
Reputation: 30442
The default manifest files (application.js
, etc) do require_tree .
which will load all files on all pages, concat them all together in production, etc. If you want things to be isolated you'll need to put a test in your ready
handler to skip this code in some cases, or you'll need more manifests (and not use application.js
for example) to silo your code per page. I suggest you read every word of the asset pipeline Rails Guide very carefully... required reading!
Upvotes: 1