多文件与coffeescript通信 [英] Multiple Files communication with coffeescript

查看:112
本文介绍了多文件与coffeescript通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个新的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:

CoffeeScript:

class ChatService
  constructor: (@io) ->

生成的JavaScript:

Generated Javascript:

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

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

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

推荐答案

根据这是客户端还是服务器端代码,稍微不同的方法。

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

客户端:这里我们将应该可用的文件附加到全局命名空间( window )如下:

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

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

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

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

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

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

注意:如果你从ChatService.coffee得到多个类,这是CoffeeScript的dict解包真正闪耀的地方,例如:

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')


$ b b




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


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

得到它:

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

第二个块的else子句可以跳过,因为 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

然后将它们附加为 module.exports = self 在客户端上替换 _。extend $ c>与另一个 extend 函数)。

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).

这篇关于多文件与coffeescript通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆