An unobtrusive way to make $ (document) .ready (.....) in rails 3

In one of my views, I need to do something when the page loads (load google maps JS and then initialize some of my things) I can, of course, insert this bit into my haml / erb template, but is there any way UJS can do it right ?

-content_for :head do = google_loader_tag #map_canvas :javascript $(document).ready(function() { google.load('maps', '3', {other_params:'sensor=false', callback: initialize}); }); 

I really thought UJS does not mean mixing Javascript and html templates together, but here I see no other choice. What do you think?

+4
source share
2 answers

as Confusion says in the comments - this is the UJS path

I would add that it is good practice to have such things inside content_for :document_ready and then yield :document_ready at the bottom of the <body> in your application layout

+3
source

Your code does not show an example of "intrusive javascript". So this is the way UJS do it.

+3
source

Source: https://habr.com/ru/post/1337646/


All Articles