Ruby on Rails - how execution is required in application.js

I am reading a book for Ruby on Rails. In "application.js" we include other JavaScript libraries as follows and more specific - jQuery UI:

//= require jquery //= require jquery_ujs //= require jquery-ui 

How is it a regular JavaScript file (and not ruby ​​extensions here like "html.erb" for exmaple), how does the application know to execute the require command? Which JavaScript syntax is this:

 //= 

and since this is a regular JavaScript file, why don't we use script tags to include JavaScript files?

In addition, I read here that the “require” method checks this library against $ LOAD_PATH variable folders. How can I see where jquery-ui is stored? I ask, because in other applications to use jQuery UI I have to add not only a JavaScript file, but also a css file and images used by the library - in this case we do this with only one line?

+4
source share
1 answer

What is JavaScript syntax.

Everything starting with // is a Javascript comment.

How can this be handled?

Server-side asterisks scan the JS file for directives . //= - special Sprocket directive. When it encounters this directive, it requests the Directive Processor to process the require command in this example. In the absence of Sprockets, the line //= require .. will be a simple JS comment.

Ruby require vs Sprockets require

These are two completely different things. Link to Ruby required.

Why not use script tags to load JS files.

Usually you want to merge all the JS files of your application and then minimize them into 1 JS master file and then include this. I recommend reading YSlow's best recommendations on this.

I also recommend looking at Railscasts Pipline Asset - http://railscasts.com/episodes/279-understanding-the-asset-pipeline

Hooray!

+10
source

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


All Articles