javascript - Ruby on Rails - how require is executed in application.js - Stack Overflow

I am reading a book for "Ruby on Rails". In the "application.js", we are including

I am reading a book for "Ruby on Rails". In the "application.js", we are including other JavaScript libraries in the following way and more specific - jQuery UI:

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

As this is ordinary JavaScript file (not ruby extensions here like "html.erb" for exmaple) how the application knows to execute the require mand? What kind of JavaScript syntax is this:

//= 

and as this is ordinary JavaScript file, why we are not using "script" tags to include JavaScript files?

Also, I have read here that "require" method will check for this library in define by $LOAD_PATH variable folders. How can I see where the "jquery-ui" is stored? I am asking because in other applications in order to use jQuery UI I should add not only JavaScript file, but css file and images used by the library - in this case, we are doing this only with single line?

I am reading a book for "Ruby on Rails". In the "application.js", we are including other JavaScript libraries in the following way and more specific - jQuery UI:

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

As this is ordinary JavaScript file (not ruby extensions here like "html.erb" for exmaple) how the application knows to execute the require mand? What kind of JavaScript syntax is this:

//= 

and as this is ordinary JavaScript file, why we are not using "script" tags to include JavaScript files?

Also, I have read here that "require" method will check for this library in define by $LOAD_PATH variable folders. How can I see where the "jquery-ui" is stored? I am asking because in other applications in order to use jQuery UI I should add not only JavaScript file, but css file and images used by the library - in this case, we are doing this only with single line?

Share Improve this question asked Nov 4, 2012 at 11:09 gotqngotqn 43.7k46 gold badges165 silver badges254 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

What kind of JavaScript syntax is this.

Anything starting with a // is a Javascript ment.

How is it able to process it ?

Sprockets on the server side scans the JS file for directives. //= is a special Sprocket directive. When it encounters that directive it asks the Directive Processor to process the mand, require in this example. In the absence of Sprockets the //= require .. line would be a simple JS ment.

Ruby require vs Sprockets require

These are two pletely different things. The one you link to is Ruby's require.

Why not use script tags to load JS files.

Usually, you want to concatenate all your app JS files and then minify them into 1 master JS file and then include that. I remend reading the YSlow best practices on this.

I also remend watching the Railscasts on Asset Pipline - http://railscasts./episodes/279-understanding-the-asset-pipeline

Cheers!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744388605a4571792.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信