简体   繁体   中英

CoffeeScript variable scoping and inheritance in Meteor > 1.0

I couldn't find anything about using @(this) to make variables global in official Meteor documentation of CS package.

  1. Is it deprecated in new versions? Should we only use shared variable now?
  2. Why haven't they also supported something like node.js require beside their smart packages and smart import policy?
  3. If someone wants to write an OOP code by Coffee he should expose all classes to whole application. or he should make a costume package. are there any other ways?

I've recently started working with Meteor, and since I work with CoffeeScript too, I've also had issues with scoping.

Having two scripts, client.coffee and server.coffee . I've declared a collection in server , that should have been autopublished, and accessible from client too. Because of scoping, my collection variable wasn't visible outside of server.coffee .

In CoffeeScript, you can write "pure JavaScript"!

localCollection = new Mongo.Collection 'MyCollection' # this is not accessible outside of server.coffee

`globalCollection = localCollection;` # this, however, is...

In CoffeeScript, everything that is enclosed in backticks is considered just JavaScript. The parser will take it as it is and put it into it's correct place in the resulting JavaScript file. Thanks to that, it won't generate var globalCollection; at the beginning of the file, and thanks to that the variable goes to package, or global scope .

Also...
Packages have export , and there is also the the share object . I've tried both, but somehow I couldn't get them to work. I'm really new to Meteor, haven't played with these two things extensively yet...

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