使用 CoffeeScript 进行多文件通信
当我创建新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据这是客户端代码还是服务器端代码,有两种略有不同的方法。
客户端:在这里,我们将跨文件可用的内容附加到全局命名空间 (
window
),如下所示:然后,在另一个文件中
ChatService
和window .ChatService
将允许访问该类。服务器端:这里我们必须使用
exports
和require
。在ChatService.coffee
文件中,您将具有以下内容:然后,要从另一个文件中获取它,您可以使用:
注意:如果您从 ChatService.coffee 获取多个类,则此这是 CoffeeScript 的 dict 解包真正发挥作用的地方,例如:
两者:基本上,我们根据所处的环境选择是运行服务器端还是客户端代码。一种常见的方法:
获取它:
的 else 子句可以跳过第二个块,因为
ChatService
已经引用了附加到window
的引用。如果您要在此文件中定义很多类,那么定义它们可能会更容易,如下所示:
然后将它们附加到服务器上,如
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:Then, in another file both
ChatService
andwindow.ChatService
will allow access to the class.Server-side: Here we must use
exports
andrequire
. In theChatService.coffee
file, you would have the following:Then, to get at it from another file you can use:
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:
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:
To get it:
The else clause of the second block can be skipped, since
ChatService
already refers to the reference attached towindow
.If you're going to define a lot of classes in this file, it may be easier to define them like:
And then attach them like
module.exports = self
on the server and_.extend(window, self)
on the client (replace_.extend
with anotherextend
function as appropriate).通常的方法是在
window
中定义一个全局命名空间:在其他任何事情发生之前,它会进入应用程序初始化代码中的某个位置。然后,对于您的类:
这允许您在任何您想要的地方通过
App
引用您的类,并且不必担心污染全局命名空间:如果您想让您的
ChatService
真正全局,那么您可以使用class window.ChatService
但我建议不要这样做,除非在最琐碎的应用程序中。AFAIK,node.js 有类似于
window
的东西,但我对 node.js 不够熟悉,无法告诉你它是什么。The usual approach is to define a global namespace in
window
:That would go somewhere in your application's initialization code before anything else happens. And then, for your class:
That allows you to reference your class through
App
anywhere you want and you don't have to worry about polluting the global namespace:If you wanted to make your
ChatService
truly global then you could useclass 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.使用命名空间分隔您的类并使用 cake< /a> 将它们全部编译到一个(或多个)生成的 .js 文件中。 Cakefile 用作控制咖啡脚本编译顺序的配置 - 对于较大的项目非常方便。
Cake 非常容易安装和设置,在编辑项目时从 vim 调用 cake 非常简单
,您可以刷新浏览器并查看结果。
由于我也忙于学习构建文件的最佳方法以及将咖啡脚本与主干和蛋糕结合使用,因此我创建了一个 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
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.