Meteor >0.6.0 和 CoffeeScript 的全局类 [英] Global classes with Meteor >0.6.0 and CoffeeScript

查看:32
本文介绍了Meteor >0.6.0 和 CoffeeScript 的全局类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从 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 未定义

ReferenceError: Foo is not defined

这是我的问题:如何使用 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?

推荐答案

docs 的 CoffeeScript 部分所述:

可以在 CoffeeScript 中使用 this 设置全局变量(或CoffeeScript 的@速记)

Global variables can be set in CoffeeScript by using this (or CoffeeScript's @ shorthand)

事实证明,CoffeeScript 类可以这样定义:

As it turns out, CoffeeScript classes can be defined like:

class @Foo

编译为:

this.Foo = (function() {
  function Foo() {}
  return Foo;
})();

假设 foo.coffeesubfoo.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 的全局类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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