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

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

问题描述

自从发布Meteor 0.6.0和添加文件级JavaScript变量范围,我遇到了一个使用CoffeeScript类的问题,每个类都在自己的相应文件中定义。



foo.coffee:

  class Foo 
...

subfoo.coffee:

  extends Foo 
...

像预期的那样, .0,我得到以下错误:


ReferenceError:Foo未定义




这里是我的问题:如何使用CoffeeScript和Meteor> 0.6.0处理跨文件的类定义?理想情况是:有没有方便的方法修改太多的类定义方式,以确保这些定义(和我的应用程序的核心部分)不依赖于Meteor?

解决方案

正如 docs 的CoffeeScript部分所述, a>:


全局变量可以在CoffeeScript中使用(或
CoffeeScript的@简写)来设置

$事实证明,CoffeeScript类可以定义为:

 <$> 




< c $ c> class @Foo

其编译为:

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

假设 foo.coffee subfoo.coffee 你可以这样做:

  class @Subfoo extends Foo 

假设 Subfoo 被分配到全局范围。还值得一提的是,您需要以类似的方式公开您的集合。例如:

  @Players = new Meteor.Collection玩家
pre>

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:

class Foo
  ...

subfoo.coffee:

class Subfoo extends Foo
  ...

As expected, and because of the changes introduced in Meteor 0.6.0, I'm getting the following error:

ReferenceError: Foo is not defined

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?

解决方案

As noted in the CoffeeScript section of the docs:

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

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

class @Foo

which compiles to:

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

Assuming that foo.coffee is loaded before subfoo.coffee you can then do:

class @Subfoo extends Foo

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天全站免登陆