使用CoffeeScript时,客户端无法访问Meteor Collection

时间:2014-03-21 08:57:17

标签: javascript coffeescript meteor

如果我使用简单的.js文件并在其中创建一个集合

Posts = new Meteor.Collection("posts");
if(Meteor.isClient){
...
...
}

我可以在浏览器中访问“posts”集合,即

> Posts.find().count();
  4

但如果我使用CoffeScript而不是JavaScript

Posts = new Meteor.Collection "posts"
if Meteor.isClient
 ...
 ...

我无法访问并且它会抛出错误

> Posts.find().count();
  ReferenceError: Posts is not defined

因为默认情况下CoffeeScript变量是文件范围的......如何克服这个问题..?

1 个答案:

答案 0 :(得分:2)

  

因为默认情况下CoffeeScript变量是文件范围的......我怎么能   克服了这个问题..?

使用@作为前缀,它将在JavaScript中编译为this.,因此它将被添加到window对象,这是JavaScript中客户端的全局作用域。

@Posts = new Meteor.Collection("posts");