This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
John Marstall
Posted Jun 09 - Read on Facebook

Setting a simple boolean in a module -- how?

My module exports a boolean in the constructor:

@options.gloss ?= false
exports.gloss

Then uses that to set a layer's visibility. But changing the boolean from app.coffee has no effect.

(Simple sample project attached)

10 Comments

John Marstall

I think I'm just really confused about when to use @ and when not to. It works if I remove the @'s. In my real project, it's @options.gloss ?= false, exports.gloss, and @gloss when I want to reference the value...

Chris Camargo

What's the big picture of what you're trying to do here? What will this boolean do, and how would you like to use it?

John Marstall

It will simply hide or show a layer. So it's something like

gloss = new Layer
visible: @gloss

John Marstall

I've actually got it working, I just wish I could understand when to use @ and when not to.

Chris Camargo

@ is just a shortcut for "this." in CoffeeScript.

John Marstall

Right, but I see modules go both ways with this. Is there a difference between these?

constructor: (@options={}) ->
@options.myVariable ?= 50

constructor: (options={}) ->
options.myVariable ?= 50

Chris Camargo

Yeah. The former is accessible via "myClass.options" and the latter is not. In your second example, options is not being attached to your class.

John Marstall

okay, thanks!

Chris Camargo

Basically, your usage of @ depends on whether or not you will be accessing these things from outside the scope of your module. If you will, it's a good idea to use it. I generally do.

John Marstall

Yes, that's generally what I want too. Here's a related example that doesn't work as I'd expect. AFAICT, the console should print "external value" but it prints "internal value only".

http://pastebin.com/APH7N4q9

Read the entire post on Facebook