在meteor 0.6.4.1 / coffeescript中,变量的可见性如何工作? [英] In meteor 0.6.4.1/coffeescript, how does variable visibility work?

查看:111
本文介绍了在meteor 0.6.4.1 / coffeescript中,变量的可见性如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的流星和咖啡。我使用非官方流星FAQ中建议的文件布局。在文件集/ C.coffee中,我有

I'm new to meteor and coffeescript. I'm using the file layout suggested in the Unofficial Meteor FAQ. In file collections/C.coffee, I have

C = new Meteor.Collection 'C'
console.log "C: #{C}"

在文件服务器/ main.coffee中, >

In file server/main.coffee, I have

C.insert {test: 'test'}

b
$ b

当我开始meteor时,我在控制台上看到:

When I start meteor, I see on the console:

C: [object Object]
ReferenceError: C is not defined
    at app/server/main.coffee.js:5:1
    at /home/xxx/yyy/.meteor/local/build/server/server.js:298:12

如何使C在集合/ C之外的文件中可用。咖啡?

How do I make C available in files outside of collections/C.coffee?

更新:将@添加到C可解决顶级问题。但是它仍然失败:

Update: Adding @ to C fixes the problem at the top level. However it still fails with:

   Meteor.methods
        test: (statement) ->
             @C.insert {test: 'test'}

code> TypeError:无法调用未定义的方法insert

It fails with an error TypeError: Cannot call method 'insert' of undefined

推荐答案

可见的外部文件,它被定义在使用 @ ,它编译为 this。在js中,它给出了与全局范围相同的效果:

To make C visible outside the file it was defined in use @, which compiles to this. or window. in js, which gives it the same effect as a global scope:

@C = new Meteor.Collection 'C'

这篇关于在meteor 0.6.4.1 / coffeescript中,变量的可见性如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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