<legend id='pQxMi'><style id='pQxMi'><dir id='pQxMi'><q id='pQxMi'></q></dir></style></legend>
      1. <small id='pQxMi'></small><noframes id='pQxMi'>

          <bdo id='pQxMi'></bdo><ul id='pQxMi'></ul>
      2. <tfoot id='pQxMi'></tfoot>
      3. <i id='pQxMi'><tr id='pQxMi'><dt id='pQxMi'><q id='pQxMi'><span id='pQxMi'><b id='pQxMi'><form id='pQxMi'><ins id='pQxMi'></ins><ul id='pQxMi'></ul><sub id='pQxMi'></sub></form><legend id='pQxMi'></legend><bdo id='pQxMi'><pre id='pQxMi'><center id='pQxMi'></center></pre></bdo></b><th id='pQxMi'></th></span></q></dt></tr></i><div id='pQxMi'><tfoot id='pQxMi'></tfoot><dl id='pQxMi'><fieldset id='pQxMi'></fieldset></dl></div>

        与咖啡脚本的多个文件通信

        Multiple Files communication with coffeescript(与咖啡脚本的多个文件通信)
        1. <small id='ZL5Cg'></small><noframes id='ZL5Cg'>

          <tfoot id='ZL5Cg'></tfoot>
            • <i id='ZL5Cg'><tr id='ZL5Cg'><dt id='ZL5Cg'><q id='ZL5Cg'><span id='ZL5Cg'><b id='ZL5Cg'><form id='ZL5Cg'><ins id='ZL5Cg'></ins><ul id='ZL5Cg'></ul><sub id='ZL5Cg'></sub></form><legend id='ZL5Cg'></legend><bdo id='ZL5Cg'><pre id='ZL5Cg'><center id='ZL5Cg'></center></pre></bdo></b><th id='ZL5Cg'></th></span></q></dt></tr></i><div id='ZL5Cg'><tfoot id='ZL5Cg'></tfoot><dl id='ZL5Cg'><fieldset id='ZL5Cg'></fieldset></dl></div>
              • <bdo id='ZL5Cg'></bdo><ul id='ZL5Cg'></ul>
                <legend id='ZL5Cg'><style id='ZL5Cg'><dir id='ZL5Cg'><q id='ZL5Cg'></q></dir></style></legend>
                    <tbody id='ZL5Cg'></tbody>

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

                  问题描述

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

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

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

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

                  Server-side:这里我们必须使用exportsrequire.在 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')
                  

                  <小时>

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


                  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 已经引用了附加到 window 的引用.

                  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(window, self) (替换 _.extend 与另一个 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).

                  这篇关于与咖啡脚本的多个文件通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                  <small id='efB1g'></small><noframes id='efB1g'>

                    <i id='efB1g'><tr id='efB1g'><dt id='efB1g'><q id='efB1g'><span id='efB1g'><b id='efB1g'><form id='efB1g'><ins id='efB1g'></ins><ul id='efB1g'></ul><sub id='efB1g'></sub></form><legend id='efB1g'></legend><bdo id='efB1g'><pre id='efB1g'><center id='efB1g'></center></pre></bdo></b><th id='efB1g'></th></span></q></dt></tr></i><div id='efB1g'><tfoot id='efB1g'></tfoot><dl id='efB1g'><fieldset id='efB1g'></fieldset></dl></div>
                        <tbody id='efB1g'></tbody>
                          <legend id='efB1g'><style id='efB1g'><dir id='efB1g'><q id='efB1g'></q></dir></style></legend>
                          • <bdo id='efB1g'></bdo><ul id='efB1g'></ul>

                            <tfoot id='efB1g'></tfoot>