如何使用选项“--bare”在 Rails 3.1 中使用 CoffeeScript?
有人知道如何在 Rails 3.1 中使用此选项吗? 现在,CoffeScript 在每个文件上放置了一个带有 .call(this)
的函数,但我想删除它。
编辑: Rails 3.1 出现“找不到变量”错误Coffeescript”和“CoffeeScript模块的模式”有我想要的想。我将更改全局变量以使用 @global
范围。
Someone know how can I use this option in Rails 3.1?
Now CoffeScript puts a function with .call(this)
on each file, but I want to remove this.
EDIT:
"Can't find variable” error with Rails 3.1 and Coffeescript" and "Pattern for CoffeeScript modules" have what I want. I'll change my global vars to use @global
scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要这样做。请参阅我的答案 CoffeeScript 模块的模式 了解其中的一些原因。 (“使您的 CoffeeScript 代码与开箱即用的 Rails 3.1 不兼容”是另一个原因。)
最好只使用或什
当您尝试导出某些内容时, 至代替
a = b
到全球范围。在 Rails 3.1 的早期版本中,启用了裸编译。这是归类为错误,并在 RC1 中修复。
因此,虽然我强烈建议您不要这样做,但重新打开裸编译的方法如下:添加
到您的
environment.rb
。I'd recommend against doing this. See my answer at Pattern for CoffeeScript modules for some of the reasons why. ("Making your CoffeeScript code incompatible with out-of-the-box Rails 3.1" is yet another reason.) Better to just use
or even
instead of
a = b
when you're trying to export something to global scope.In previous versions of Rails 3.1,
bare
compilation was enabled. This was classified as a bug, and fixed in RC1.So while I strongly encourage you not to do this, here's how to turn bare compilation back on: Add
to your
environment.rb
.我确实建议利用 CoffeeScript 的闭包并遵循 CommonJS 模块模式。但有时,只是有时,使用 --bare 选项是可以的。就我而言,在渲染 Jasmine 规范助手时,我可以将事情保持在顶层,并利用所述 Jasmine 规范中的 include Sprockets 指令。
为此,我创建了“sprockets-blackcoffee”gem,您可以在这里了解它。 https://github.com/metaskills/sprockets-blackcoffee
I do recommend taking advantage of CoffeeScript's closures and following a CommonJS module patter. But sometimes, just sometimes, it is OK to want to use the --bare option. In my case, when rendering a Jasmine spec helper so I could keep things at the top level and also take advantage of the include Sprockets directive in said Jasmine specs.
To that end, I created the "sprockets-blackcoffee" gem, which you can learn about here. https://github.com/metaskills/sprockets-blackcoffee