为什么 Rails 在 CoffeeScript 文件不能包含 javascript 代码时使用 .js.coffee 扩展名?
我发现这相当具有误导性,因为我认为它表明此类文件可能包含 javascript 和 Coffeescript 代码的混合。我是否缺少一些非常基本的东西?
I found that to be rather misleading as I thought it suggest that such files could include a mixture of both javascript and coffeescript code. Is there something very fundamental that I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 中的常见做法是模板具有
.js.coffee
、.html.erb
、.html.haml
等扩展名。如果我没记错的话,Rails 将这些扩展解释为
.[format].[builder]
并使用这些知识来做两件事:[format]
值找到正确的模板具有请求中列出的可接受的格式接受
标头;It's a common practice in Rails for templates to have extensions like
.js.coffee
,.html.erb
,.html.haml
, etc.If I remember correctly Rails interprets these extensions as
.[format].[builder]
and uses that knowledge to do two things:[format]
value comparing it with acceptable formats listed in the request'sAccept
header;[builder]
value to parse your template.如果使用反引号,则可以包含纯 JavaScript。
像
`function(){ do some };`
之类的东西会起作用。And you can include plain javascript if you use backticks.
Something like
`function(){ do something };`
would work.