使用 CoffeeScript 进行多文件通信

发布于 2025-01-05 21:16:31 字数 480 浏览 1 评论 0原文

当我创建新的 CoffeeScript 文件时,我无法从另一个文件访问已编译代码中的代码,因为它被包装在某些函数范围中。例如:

CoffeeScript:

class ChatService
  constructor: (@io) ->

生成的 Javascript:

(function() {
  var ChatService;    
  ChatService = (function() {    
    function ChatService(io) {
      this.io = io;
    }    
    return ChatService;    
  })();    
}).call(this);

当尝试在另一个文件中调用 ChatService 时,它未定义。如何使用 CoffeeScript 处理多个文件?

When I create a new coffeescript file, I cannot access the code in the compiled code from another file because it gets wrapped in some function scope. For example:

CoffeeScript:

class ChatService
  constructor: (@io) ->

Generated Javascript:

(function() {
  var ChatService;    
  ChatService = (function() {    
    function ChatService(io) {
      this.io = io;
    }    
    return ChatService;    
  })();    
}).call(this);

When trying to call ChatService in another file, it's not defined. How do I handle multiple files with coffeescript?

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

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

发布评论

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

评论(3

淡紫姑娘! 2025-01-12 21:16:31

根据这是客户端代码还是服务器端代码,有两种略有不同的方法。

客户端:在这里,我们将跨文件可用的内容附加到全局命名空间 (window),如下所示:

class window.ChatService
  constructor: (@io) ->

然后,在另一个文件中 ChatServicewindow .ChatService 将允许访问该类。


服务器端:这里我们必须使用exportsrequire。在 ChatService.coffee 文件中,您将具有以下内容:

class exports.ChatService
  constructor: (@io) ->

然后,要从另一个文件中获取它,您可以使用:

ChatService = require('ChatService.coffee').ChatService

注意:如果您从 ChatService.coffee 获取多个类,则此这是 CoffeeScript 的 dict 解包真正发挥作用的地方,例如:

{ChatService, OtherService} = require('ChatService.coffee')

两者:基本上,我们根据所处的环境选择是运行服务器端还是客户端代码。一种常见的方法:

class ChatService
  constructor: (@io) ->

if typeof module != "undefined" && module.exports
  #On a server
  exports.ChatService = ChatService
else
  #On a client
  window.ChatService = ChatService

获取它:

if typeof module != "undefined" && module.exports
  #On a server
  ChatService = require("ChatService.coffee").ChatService
else
  #On a client
  ChatService = window.ChatService

的 else 子句可以跳过第二个块,因为 ChatService 已经引用了附加到 window 的引用。

如果您要在此文件中定义很多类,那么定义它们可能会更容易,如下所示:

self = {}

class self.ChatService

然后将它们附加到服务器上,如 module.exports = self_.在客户端上扩展(window, self)(根据需要将_.extend替换为另一个extend函数)。

Depending on whether this is client- or server-side code, there are two slightly different approaches.

Client-side: Here we attach things that should be available across files to the global namespace (window) as follows:

class window.ChatService
  constructor: (@io) ->

Then, in another file both ChatService and window.ChatService will allow access to the class.


Server-side: Here we must use exports and require. In the ChatService.coffee file, you would have the following:

class exports.ChatService
  constructor: (@io) ->

Then, to get at it from another file you can use:

ChatService = require('ChatService.coffee').ChatService

Note: If there are multiple classes that you are getting from ChatService.coffee, this is one place where CoffeeScript's dict unpacking really shines, such as:

{ChatService, OtherService} = require('ChatService.coffee')

Both: Basically, we choose whether to run server-side or client-side code based on which environment we're in. A common way to do it:

class ChatService
  constructor: (@io) ->

if typeof module != "undefined" && module.exports
  #On a server
  exports.ChatService = ChatService
else
  #On a client
  window.ChatService = ChatService

To get it:

if typeof module != "undefined" && module.exports
  #On a server
  ChatService = require("ChatService.coffee").ChatService
else
  #On a client
  ChatService = window.ChatService

The else clause of the second block can be skipped, since ChatService already refers to the reference attached to window.

If you're going to define a lot of classes in this file, it may be easier to define them like:

self = {}

class self.ChatService

And then attach them like module.exports = self on the server and _.extend(window, self) on the client (replace _.extend with another extend function as appropriate).

浸婚纱 2025-01-12 21:16:31

通常的方法是在 window 中定义一个全局命名空间:

window.App = { }

在其他任何事情发生之前,它会进入应用程序初始化代码中的某个位置。然后,对于您的类:

class App.ChatService
  constructor: (@io) ->

这允许您在任何您想要的地方通过 App 引用您的类,并且不必担心污染全局命名空间:

chatter = new App.ChatService

如果您想让您的 ChatService 真正全局,那么您可以使用 class window.ChatService 但我建议不要这样做,除非在最琐碎的应用程序中。

AFAIK,node.js 有类似于 window 的东西,但我对 node.js 不够熟悉,无法告诉你它是什么。

The usual approach is to define a global namespace in window:

window.App = { }

That would go somewhere in your application's initialization code before anything else happens. And then, for your class:

class App.ChatService
  constructor: (@io) ->

That allows you to reference your class through App anywhere you want and you don't have to worry about polluting the global namespace:

chatter = new App.ChatService

If you wanted to make your ChatService truly global then you could use class window.ChatService but I'd recommend against that except in the most trivial of applications.

AFAIK, node.js has something similar to window but I'm not familiar enough with node.js to tell you what it is.

固执像三岁 2025-01-12 21:16:31

使用命名空间分隔您的类并使用 cake< /a> 将它们全部编译到一个(或多个)生成的 .js 文件中。 Cakefile 用作控制咖啡脚本编译顺序的配置 - 对于较大的项目非常方便。

Cake 非常容易安装和设置,在编辑项目时从 vim 调用 cake 非常简单

:!cake build

,您可以刷新浏览器并查看结果。

由于我也忙于学习构建文件的最佳方法以及将咖啡脚本与主干和蛋糕结合使用,因此我创建了一个 github上的小项目作为我自己的参考,也许它也会对你在蛋糕和一些基本的事情上有所帮助。所有编译后的文件都位于www文件夹中,以便您可以在浏览器中打开它们,所有源文件(除了蛋糕配置)都位于src文件夹中。在此示例中,所有 .coffee 文件均被编译并合并为一个输出 .js 文件,然后该文件包含在 html 中。

Separate your classes with namespaces and use cake to compile them all in one (or more) resulting .js file(s). Cakefile is used as configuration which controls in which order your coffee scripts are compiled - quite handy with bigger projects.

Cake is quite easy to install and setup, invoking cake from vim while you are editing your project is then simply

:!cake build

and you can refresh your browser and see results.

As I'm also busy to learn the best way of structuring the files and use coffeescript in combination with backbone and cake, I have created a small project on github to keep it as a reference for myself, maybe it will help you too around cake and some basic things. All compiled files are in www folder so that you can open them in your browser and all source files (except for cake configuration) are in src folder. In this example, all .coffee files are compiled and combined in one output .js file which is then included in html.

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