3
I have an app in Rails 4 and after scaffolding it generates a *.js.coffee for each model, as I am not using coffeescript renamed to *.js and am trying to use jQuery. The problem is that I need to trigger a process delayed by ajax after loading the page itens/show/x
and I did it this way
$(document).ready(function() {
$.ajax({
url:'/get_content_recommendation/' + gon.item_id + '.js',
type:"get"
});
});
The problem is that it is running on all pages of the app, because of the <%= javascript_include_tag "application", media: "all" %>
in the layout before the <%= yield %>
What is the best solution to separate jQuery by view, is there any Gem or any good practice? This is the best way to fire process by ajax after page loading?
How nice! This way it is possible to use separate the javascript inside each . js and keep the view free of javascript. I must have
$(document).ready(function() {...});
for each command, only 1 per file or it is not required?– Vitor Vezani
@Awkward If you are calling the script tag in <head> (necessary with turbolinks), you will have to use yes, it is okay to call several times. If calling at the bottom of the page before the <body> there does not need.
– user7261