Efficient way to manage javascript / coffeescript code for Rails 5?

I am building a web application in Rails 5.0.2. I have the following JS files for my project:

enter image description here

In addition, I have a similar model for each of my own JS files, as follows:

$(function () {
    var init = function () {
        // my code, I want to run on specific page load i.e. /remarks
        $('form#cf_remarks_form').validate(validate_options);
    };
    init();
    document.addEventListener("turbolinks:load", function () {
        init();
    });
});

I have the following disclaimers / question about using JS assets:

  • Rails by default attach (and as a result run) all js files on all pages for one layout. Is it good to add only the appropriate js to certain resources / pages using a custom template or go by default?

  • js , / jQuery $('div.custom input.no-search') , , .

  • Rails 5 ajax . company.js /, say/home. /, , company.js, . , turbolinks: UI , turbolinks: , . ( )

, , js- , Rails 5? , , .

+4
1

:

  • . - , . , , js , , . .
  • , , , .remarks_form, js , ( , ).
  • js , , , - init(), turbolinks turbolinks: . , , , js.

5:

document.addEventListener("turbolinks:load", function() {
    console.log('Loads each time');
});

:

http://guides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks

. js , , , , . Rails , , , , 50 js . YMMV, , , , Rails.

Rails , , . js , ( ):

  • js , js inline , .
  • js , , (, js ).
  • js - . .
+1

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


All Articles