2014-11-06 105 views
0

我已经看到加载顺序上的一些事情,但事情似乎已经改变,因为1.0出来。让我说明我所看到的,也许有人可以告诉我我做错了什么:加载顺序:Meteor.js和Coffeescript

+ root-dir 
| 
+ - other stuff 
| 
+ main.coffee <- global declarations 
| 
+ lib 
    | 
    + - charger_collection.coffee 
    | 
    +- charger_schema.coffee 

这应该几乎覆盖它。现在,我意识到这不是目前的最佳做法,我不反对改变,但我需要了解为什么会发生以下问题。 main.coffee包含此:

# Share globably accessible stuff like collections by 
# attaching to 'this' 

@Chargers = new Mongo.Collection("chargers") 

和引用@Chargers其他文件可以作为你所期望的。例如: -

# client/chargers.coffee 

Template.chargers.helpers 
    list: -> 
    return Chargers.find({}, {limit:50}) 

然而,问题就来了下面的使用行为,Collection2(节选)的优势:如果我使用@ChargersChargers发生

# lib/charger_schema.coffee 

@Schemas = {} 

@Schemas.Charger = new SimpleSchema 
    id: 
    type: Number 
    optional: false 
    name: 
    type: String 
    label: "Name" 
    max: 200 
    site_id: 
    type: Number 
    label: "Site ID" 
    "address.street": 
    type: String 
    label: "Street" 
    "address.city": 
    type: String 
    label: "City" 
    optional: true 

@Chargers.attachSchema Schemas.Charger # <= TypeError: Cannot call method 'attachSchema' of undefined 

此相同的错误。

两个问题:

  1. 为什么可视性问题?

  2. 如何解决问题?

谢谢!

回答

1

仔细阅读文档中的structuring your app部分。相关部分是:

与main。*匹配的所有文件都移动到其他所有文件之后,并保留其顺序。

所以,你的结构是干什么的,你想的正好相反 - 它宣布一切已加载后的集合。建议您的收藏集定义在/lib之下,以便首先加载它们。通常在/lib/collections之类的东西下,/lib中的其他文件都将具有集合定义(首先加载深度嵌套文件)。

您可能还想考虑将charger_collection.coffee,charger_schema.coffee和您的集合定义的内容移动到同一文件下,以避免它们之间的依赖性问题。或者,您可以回退嵌套的目录或命名约定(文件在同一目录中按字母顺序装入)以解决问题。