我从 Meteor 开始,我已经从一个咖啡文件中运行了所有内容,但我想组织起来并将一些代码移动到/public 和/server。但是,当我这样做时,我的集合是未定义的。我已经移动了很多代码,但集合仍然未定义。我已经删除了自动发布和不安全的软件包。我做错了什么?
/main.coffee
Trips = new Meteor.Collection "trips"
if Meteor.isClient
Meteor.subscribe 'trips'
if Meteor.isServer
Meteor.publish 'trips', -> Trips.find()
Trips.allow
insert: -> true
update: -> true
remove: -> true
/client/trips.coffee
Meteor.startup ->
Template.Trips.all_trips = -> Trips.find()
最佳答案
您需要一个 Trips
才能访问其他文件,Meteor 0.6.0 introduced variable scoping您在根目录中定义了一个 Trips
,因此它不是全局的,您的 client/trips.coffee
无法看到它。您可以使用 @
将其设置为全局:
在你的/main.coffee
@Trips = new Meteor.Collection "trips"
因此其他文件(客户端和服务器)都可以访问它。
当您进一步拆分文件时,您最终应该得到 3 个文件:
/main.coffee(服务器和客户端均可访问并首先加载)
@Trips = new Meteor.Collection "trips"
/client/trips.coffee
Meteor.subscribe 'trips'
Meteor.startup ->
Template.Trips.all_trips = -> Trips.find()
/server/server.coffee
Meteor.publish 'trips', -> Trips.find()
Trips.allow
insert: -> true
update: -> true
remove: -> true
关于meteor - 使用客户端和服务器目录组织 Meteor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15985470/