coffeescript - 在 meteor 0.6.4.1/coffeescript 中,可变可见性如何工作?

标签 coffeescript meteor

我是 meteor 和 CoffeeScript 的新手。我正在使用非官方 Meteor 常见问题解答中建议的文件布局。在文件集合/C.coffee 中,我有

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

在文件 server/main.coffee 中,我有
C.insert {test: 'test'}

当我启动 meteor 时,我在控制台上看到:
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 在 collections/C.coffee 之外的文件中可用?

更新:在 C 中添加 @ 可以解决顶层问题。但是它仍然失败:
   Meteor.methods
        test: (statement) ->
             @C.insert {test: 'test'}

它失败并出现错误 TypeError: Cannot call method 'insert' of undefined

最佳答案

为了使 C 在文件之外可见,它在使用中定义 @ ,编译为 this.window.在 js 中,这使它具有与全局范围相同的效果:

@C = new Meteor.Collection 'C'

关于coffeescript - 在 meteor 0.6.4.1/coffeescript 中,可变可见性如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18150532/

相关文章:

javascript - 无法在 'open' : Unable to open a window with invalid URL 'Window' 上执行 '%3127.0.0.1:3000'

javascript - 使用 CoffeeScript 删除数组的重复元素

javascript - jquery 检查元素是否不是 X 或者元素的父元素是否不是 X - 可以做得更简单吗?

javascript - CoffeeScript 不工作

javascript - 如何在 meteor 模板之外(即 head 标签内)实现辅助变量(模板标签)?

linux - SQLITE_IOEERR : in Meteor

javascript - Coffeescript json响应处理

javascript - 迁移到 meteor / react

javascript - 保持两个 Meteor 集契约(Contract)步

javascript - 在数组中搜索特定值