Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In meteor 0.6.4.1/coffeescript, how does variable visibility work?

I'm new to meteor and coffeescript. I'm using the file layout suggested in the Unofficial Meteor FAQ. In file collections/C.coffee, I have

C = new Meteor.Collection 'C'
console.log "C: #{C}"

In file server/main.coffee, I have

C.insert {test: 'test'}

When I start meteor, I see on the console:

C: [object Object]
ReferenceError: C is not defined
    at app/server/main.coffee.js:5:1
    at /home/xxx/yyy/.meteor/local/build/server/server.js:298:12

How do I make C available in files outside of collections/C.coffee?

Update: Adding @ to C fixes the problem at the top level. However it still fails with:

   Meteor.methods
        test: (statement) ->
             @C.insert {test: 'test'}

It fails with an error TypeError: Cannot call method 'insert' of undefined

like image 572
Jerry Avatar asked Aug 09 '13 15:08

Jerry


1 Answers

To make C visible outside the file it was defined in use @, which compiles to this. or window. in js, which gives it the same effect as a global scope:

@C = new Meteor.Collection 'C'
like image 190
Tarang Avatar answered Dec 26 '22 20:12

Tarang