问题描述
自从 Meteor 0.6.0 发布和 文件级 JavaScript 变量范围,我在使用 CoffeeScript 类时遇到了问题,每个类都在各自的文件中定义.
Since the release of Meteor 0.6.0 and the addition of file-level JavaScript variable scoping, I'm facing an issue using CoffeeScript classes, each of them being defined in its own respective file.
foo.coffee:
foo.coffee:
class Foo
...
subfoo.coffee:
subfoo.coffee:
class Subfoo extends Foo
...
正如预期的那样,由于 Meteor 0.6.0 中引入的更改,我收到以下错误:
As expected, and because of the changes introduced in Meteor 0.6.0, I'm getting the following error:
ReferenceError: Foo 未定义
这是我的问题:如何使用 CoffeeScript 和 Meteor >0.6.0 处理跨文件的类定义?理想情况下:是否有一种方便的方法不对类的定义方式进行过多修改,以确保这些定义(以及我的应用程序的核心部分)不依赖于 Meteor?
Here's my question: how should one handle class definitions across files with CoffeeScript and Meteor >0.6.0? Ideally: is there a convenient way not to modify too much the way classes are defined in order to make sure these definitions (and core parts of my application) are not Meteor-dependent?
推荐答案
全局变量可以在 CoffeeScript 中通过使用这个(或CoffeeScript 的 @ 速记)
事实证明,CoffeeScript 类可以定义为:
As it turns out, CoffeeScript classes can be defined like:
class @Foo
编译为:
this.Foo = (function() {
function Foo() {}
return Foo;
})();
假设 foo.coffee
在 subfoo.coffee
之前加载,你可以这样做:
Assuming that foo.coffee
is loaded before subfoo.coffee
you can then do:
class @Subfoo extends Foo
当然,假设需要将 Subfoo
分配给全局范围.还值得一提的是,您需要以类似的方式公开您的集合.例如:
Assuming, of course, that Subfoo
needs be be assigned to the global scope. It's also worth mentioning that you'll need to expose your collections in a similar way. For example:
@Players = new Meteor.Collection 'players'
这篇关于Meteor > 0.6.0 和 CoffeeScript 的全局类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!