区分 CoffeeScript 和 Ruby on Rail 资产管道的好方法是什么?

发布于 2024-11-18 06:04:17 字数 1761 浏览 3 评论 0原文

TLDR:

如何在 RoR 中将多个 CoffeeScript 文件合并到一个 JS 文件中,所有这些文件都位于同一个匿名函数块下?

长版本:

我有一些 CS 文件,将作为 RoR Web 应用程序的一部分加载。我想知道:分离 CoffeeScripts 和 Ruby on Rail 3.1 资产管道的好方法是什么?

让我们使用以下代码作为示例代码:

main.js.coffee

window.MyApp = {} # to escape the CoffeeScript anonymous function block
# (I like the anonymous function block because it protects my other

MY_GLOBAL_SETTING = "world!"
$.click "#my_button" myApp.sayHello
# (I could use something like goog.bind here instead of using myApp. Any suggestions? Fat arrow?)

hello.js.coffee

MyApp.sayHello = sayHello () ->
  doComplicatedStuff()
  alert("Hello #{ MY_GLOBAL_SETTING }")

complicated.js.coffee

doComplicatedStuff = () ->
  # some really complicated algorithm, for example
  true

我的资产目录结构如下如下所示:

assets/
  application.js
  application/
    # javascript that gets used with the main application
  secondary_page.js
  secondary_page/
    complicated.js.coffee
    hello.js.coffee
    main.js.coffee

secondary.js

//= require secondary_page/main.js.coffee
//= require secondary_page/complicated.js.coffee
//= require secondary_page/hello.js.coffee

作为构建过程的一部分,我曾经将这些文件与 CoffeeScript 一起编译,但现在我想改用资源管道。我正在喝 RoR 3.1 kool-aid!哈哈,说真的,资产管道看起来很棒。

我遇到的问题是 secondary.js 如下所示:

(function() {
  // main.js
).call(this);
(function() {
  // complicated.js
).call(this);
(function() {
  // hello.js
).call(this);

这会阻止局部变量在整个代码之间共享。 MY_GLOBAL_SETTING 和 doComplicatedStuff 无法用于 sayHello。

那么……我该怎么办?如果不再次引入我自己的自定义编译步骤,我想不出什么好方法。

TLDR:

What can you do to combine multiple CoffeeScript files into one JS file, in RoR, all under the same anonymous function block?

Long version:

I have a few CS files that will be loaded for part of a RoR web app. I'm wondering: what is a good way to separate concerns with CoffeeScripts and Ruby on Rail 3.1's asset pipeline?

Let's use the following as example code:

main.js.coffee

window.MyApp = {} # to escape the CoffeeScript anonymous function block
# (I like the anonymous function block because it protects my other

MY_GLOBAL_SETTING = "world!"
$.click "#my_button" myApp.sayHello
# (I could use something like goog.bind here instead of using myApp. Any suggestions? Fat arrow?)

hello.js.coffee

MyApp.sayHello = sayHello () ->
  doComplicatedStuff()
  alert("Hello #{ MY_GLOBAL_SETTING }")

complicated.js.coffee

doComplicatedStuff = () ->
  # some really complicated algorithm, for example
  true

I have my assets directory structured like the following:

assets/
  application.js
  application/
    # javascript that gets used with the main application
  secondary_page.js
  secondary_page/
    complicated.js.coffee
    hello.js.coffee
    main.js.coffee

secondary.js

//= require secondary_page/main.js.coffee
//= require secondary_page/complicated.js.coffee
//= require secondary_page/hello.js.coffee

I used to compile the files together with CoffeeScript as part of the build process, but now I want to use the asset pipeline instead. I'm drinking the RoR 3.1 kool-aid! Haha, seriously though, the asset pipeline looks awesome.

The problem I'm experiencing is that secondary.js looks like the following:

(function() {
  // main.js
).call(this);
(function() {
  // complicated.js
).call(this);
(function() {
  // hello.js
).call(this);

This prevents local variables from being shared amongst the entire code. MY_GLOBAL_SETTING and doComplicatedStuff aren't available to sayHello.

So... what should I do? I can't think of a good way without introducing my own custom compilation step again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2024-11-25 06:04:17

对于开始使用 CoffeeScript 的 Rails 开发人员来说,这是一个常见问题。例如,请参阅:

解决方案比比皆是。最简单的方法是使用 @ 作为您希望在特定文件外部可见的变量声明的前缀,因为 this 将指向 window每个文件的最外层上下文,当没有定义本地 x 时,x 指向 window.x

This is a common question for Rails developers starting to use CoffeeScript. See, for example:

Solutions abound. The simplest is to preface variable declarations that you want to be visible outside of a particular file with @, since the this will point to window in the outermost context of each file, and x points to window.x when no local x is defined.

寄意 2024-11-25 06:04:17

也许最好的方法是将它们保留在自己的匿名范围内,但导出您需要从其他模块访问的各个函数。在 Trevor Burnham 的 Coffeescript 书中,他建议您这样做(适用于 Node 和浏览器):

root = global ? window
root.exportableFunction = exportableFunction
root.MY_GLOBAL_SETTING = some: 'Setting'

Probably the best way to do this is to leave them in their own anonymous scopes but export the individual functions you need access to from other modules. From Trevor Burnham's Coffeescript book, he recommends you do this (which works in node and browsers):

root = global ? window
root.exportableFunction = exportableFunction
root.MY_GLOBAL_SETTING = some: 'Setting'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文