【问题标题】:Global meteor collection in closure for CoffeescriptCoffeescript 关闭全球流星收集
【发布时间】:2013-05-31 09:26:59
【问题描述】:

如果我声明一个全局集合如下:

@Matches = new Meteor.Collection "Matches"

如何在 Meteor 的服务器端和客户端的闭包中找到一致的方式来访问它?

例如,下面的代码不起作用,因为@ 指的是this(它不是闭包中的顶级命名空间)

Meteor.publish("current-matches", ->
  return @Matches.find(round: 0)  # @Matches doesn't work since `this` is something else
)

【问题讨论】:

    标签: coffeescript meteor


    【解决方案1】:

    将您的集合定义放在一个共享目录中,以便客户端和服务器都能看到它们。然后,您可以在没有@ 的情况下使用它们。例如:

    collections/matches.coffee

    @Matches = new Meteor.Collection 'matches'
    

    server/server.coffee

    Meteor.publish 'current-matches', ->
      Matches.find round: 0
    

    【讨论】:

    • 只是一个旁注,不要使用Match 作为名称。 Match 是流星中定义的东西:docs.meteor.com/#/full/match_test 是的,我知道,你用过Matches ...
    猜你喜欢
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多