Rails 3:将 javascript 放在哪里的好经验法则?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6287440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3: good rule of thumb for where to put javascript?
提问by Eric Hu
When writing javascript for a specific page in a site, when do you want to turn the javascript into a function and include it in application.js
?
在为站点中的特定页面编写 javascript 时,您希望什么时候将 javascript 转换为函数并将其包含在application.js
?
I've seen suggestions about doing this(and minifying or gzip-ing) to minimize HTTP requests. That makes sense, but what about maintainability? If I have js code specific to one view, it seems like more work to look into a potentially massive application.js
. That code could be embedded into that view or put in its own .js
(or .js.erb
or .rjs
) file in that view folder.
我已经看到有关这样做(以及缩小或 gzip-ing)以最小化 HTTP 请求的建议。这是有道理的,但是可维护性呢?如果我有特定于一个视图的 js 代码,那么查看潜在的大量 .js 文件似乎需要更多的工作application.js
。该代码可以嵌入到该视图中,也可以放在该视图文件夹中它自己的.js
(或.js.erb
或.rjs
)文件中。
I've seen another suggestionthat Rails automatically merges all javascript into one file. Is this true?
我看到了另一个建议,即 Rails 会自动将所有 javascript 合并到一个文件中。这是真的?
TLDR: how much or how little should a developer worry about optimization when writing javascript?
TLDR:开发人员在编写 javascript 时应该担心多少优化问题?
回答by Eric Hu
As I haven't seen an answer in about a month, I'll answer this question to the best of my current knowledge.
由于我已经有大约一个月没有看到答案了,我将尽我目前所知来回答这个问题。
Rails 3.1 (currently at release candidate 4) introduces sprockets, which will compile all javascript in a rails project into one file. It even comes with tools to minify and compress javascript so that it's all delivered to the client at once.
Rails 3.1(目前是候选版本 4)引入了sprockets,它将把 rails 项目中的所有 javascript 编译成一个文件。它甚至带有用于缩小和压缩 javascript 的工具,以便一次性将其全部交付给客户端。
Related to sprockets is the Rails 3.1 asset pipeline. As I understand, it's a folder hierarchy/abstraction. Javascripts can go into 3 folders:
与链轮相关的是 Rails 3.1 资产管道。据我了解,这是一个文件夹层次结构/抽象。Javascript 可以进入 3 个文件夹:
/apps/assets/javscripts/
Javascript files specific to the application, including application.js
. This should only contain the manifest of javascript files you want to include in your project. The rails new
tool will generate this file and include jquery in the manifest.
/apps/assets/javscripts/
特定于应用程序的 Javascript 文件,包括application.js
. 这应该只包含您想要包含在项目中的 javascript 文件的清单。该rails new
工具将生成此文件并在清单中包含 jquery。
/lib/assets/javascripts/
Javascript files written by the developer that are more general purpose. (My impression is that this would be for javascript libraries you develop to drop into multiple applications)
/lib/assets/javascripts/
由开发人员编写的更通用的 Javascript 文件。(我的印象是这将用于您开发的 javascript 库以放入多个应用程序)
/vendor/assets/javascripts/
Third party javascript files (i.e. JQuery, Modernizr)
/vendor/assets/javascripts/
第三方 javascript 文件(即 JQuery、Modernizr)
All files in these folders will appear to the client inside /assets/
, abstracting out the server side file paths. I assume this is meant to help the developer organize javascript files.
这些文件夹中的所有文件都会出现在客户端里面/assets/
,抽象出服务器端的文件路径。我认为这是为了帮助开发人员组织 javascript 文件。
To answer my own question
回答我自己的问题
- Put javascript functions into separate files, group them logically. My test app indicated that subfolders within
.../assets/javascripts/
are ok if and only ifthe subfolder path is included in the manifest.- I.E. putting
//= subfolder/js_file
in the manifest will work. Putting//= js_file
will not ifjs_file
is inside.../javascripts/subfolder/
- DHH mentions a "rule of 13" in his talk (linked below). If the number of javascripts in a folder exceeds 13, it starts to look messy and unmanageable. This would be a good time to group javascript files into subfolders.
- I.E. putting
- Use the built in Rails 3.1 minifier and compressor (or install a preferred gem)
- Refactor javascript code from time to time. Move functions to
/lib/assets/javascripts/
over time. (The goal is to eventually recognize when you want to write general purpose javascript functions as opposed to application-specific ones and eliminate this refactor step)
- 将 javascript 函数放入单独的文件中,按逻辑对它们进行分组。我的测试程序表明,内子文件夹
.../assets/javascripts/
都ok当且仅当包括在清单中的子文件夹路径。- IE 放入
//= subfolder/js_file
清单将起作用。把//= js_file
会如果没有js_file
在里面.../javascripts/subfolder/
- DHH 在他的演讲中提到了“13 法则”(链接如下)。如果一个文件夹中的 javascript 数量超过 13,它就会开始看起来凌乱且难以管理。这将是将 javascript 文件分组到子文件夹中的好时机。
- IE 放入
- 使用内置的 Rails 3.1 压缩器和压缩器(或安装首选 gem)
- 不时重构 javascript 代码。
/lib/assets/javascripts/
随着时间的推移移动功能。(目标是最终识别出何时要编写通用 javascript 函数而不是特定于应用程序的函数,并消除此重构步骤)
More Resources
更多资源
a blog post covering all changes in Rails 3.1
DHH's talk on Rails 3.1 changes, May 16 2011 (~1 hr)
一篇涵盖 Rails 3.1 中所有变化的博客文章
DHH 关于 Rails 3.1 变化的演讲,2011 年 5 月 16 日(约 1 小时)