简体   繁体   中英

Load Order: Meteor.js and Coffeescript

I've seen a few things on load order but things seem to have changed since 1.0 came out. Let me illustrate what I'm seeing and maybe someone can tell me what I'm doing wrong:

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

That should pretty much cover it. Now, I realize this is not current best practices and I'm not averse to changing, but I need to understand why the following problem is occurring. main.coffee contains this:

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

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

And references to @Chargers in other files works as you might expect. Eg:

# client/chargers.coffee

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

However, the problem comes in the following usage that takes advantage of Collection2 (excerpt):

# 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

This same error occurs if I use @Chargers or Chargers .

Two questions:

  1. Why the visiblity issue?

  2. How best to fix?

Thanks!

Have a close look at the structuring your app section from the docs. The relevant section is:

All files that match main.* are moved after everything else, preserving their order.

So your structure is doing the opposite of what you want - it's declaring the collection after everything else has loaded. It's recommended that your collection definitions go under /lib so they are loaded first. Typically under something like /lib/collections so other files in /lib will all have the collection definitions (deeply nested files are loaded first).

You may also want to consider moving the contents of charger_collection.coffee , charger_schema.coffee , and your collection definition under the same file to avoid dependency issues between them. Alternatively, you can fall back on nested directories or naming conventions (files are loaded in alphabetical order within the same directory) to fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM