简体   繁体   English

将所有JavaScript加载到资产JavaScript下是否很好?

[英]Is it good to load all javascripts under asset javascripts

I wonder what's practice is better to load Javascripts in Rails app. 我想知道哪种做法更好地在Rails应用程序中加载Javascript。

Because every page will load all the js files under asset folder. 因为每个页面都会加载资产文件夹下的所有js文件。

But most of coffescripts are only apply on their own controller. 但是,大多数脚本仅适用于它们自己的控制器。

So to require_tree . 所以要require_tree . seems a bad idea, here. 似乎是个坏主意,在这里。

So I need to include corresponding coffee script for each view ? 所以我需要为每个视图包括相应的咖啡脚本?

It also seems a bad idea, and easily to cause maintain problem. 这似乎也不是一个好主意,并且容易引起维护问题。

Any idea ? 任何想法 ? Thanks 谢谢

在此处输入图片说明

Application.js 的application.js

//= require_tree .

By default Rails handles assets at the controller level and will automatically include the appropriate assets as long as they are in the right file, which corresponds to a controller. 默认情况下,Rails在控制器级别处理资产,只要适当的资产在与控制器相对应的正确文件中,它们就会自动包括这些资产。 In this case it works correctly with the //= require_tree . 在这种情况下,它可以与//= require_tree .一起正常工作//= require_tree . directive. 指示。

If you prefer, you can also include controller specific assets manually in layouts or views like so: <%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %> . 如果愿意,还可以在布局或视图中手动包括特定于控制器的资产,例如: <%= javascript_include_tag params[:controller] %><%= stylesheet_link_tag params[:controller] %> In this case your require directives should not have //= require_tree . 在这种情况下,您的require指令不应具有//= require_tree . . Remember that you will have to specify any files not in your require directive for pre-compilation in production. 请记住,在生产中进行预编译时,必须指定require指令中未包含的任何文件。

For more see the Rails documentation at http://guides.rubyonrails.org/asset_pipeline.html , under '2.1 Controller Specific Assets'. 有关更多信息,请参见http://guides.rubyonrails.org/asset_pipeline.html上的 “ 2.1控制器特定资产”下的Rails文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM