I couldn't find anything about using @(this)
to make variables global in official Meteor documentation of CS package.
shared
variable now? require
beside their smart packages and smart import policy? 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.