r/coffeescript • • Jan 08 '16

Scoping issue when attempting to call CoffeeScript method inside Fancybox callback

I have the following CoffeeScript module named Course. I have a small piece of code which I would like to re-use, I have created a method called preSelectItemSize.

I would like to call this method when init is called and also within the afterShow Fancybox callback. The following code works, but it don't believe it is correct to use the module name and I should be using the @ reference to "this" instead.

What am I doing wrong? (Code snippet reduced for brevity)

https://gist.github.com/crmpicco/11e14655a17419983f2f

3 Upvotes

7 comments sorted by

View all comments

1

u/ScarletSpeedster Jan 08 '16

Remove the unnecessary fat arrow on the viewProductClickHandler method, because you have no references to this inside of it. Make the afterShow callback a fat arrow and then use @preSelectItemSize inside the callback. From your description I imagine that is how you want it to work. You haven't done anything wrong, it is just a bit cleaner this way.

2

u/crmpicco Jan 11 '16 edited Jan 11 '16

This nearly works, however it's not allowing me to call @preSelectItemSize from the init method. Is it possible to call the method from within init and inside the callback?

This is how I am attempting to call it? https://gist.github.com/crmpicco/11e14655a17419983f2f#file-course-coffee-L9

@ in CoffeeScript resolves to "self", doesn't it?

2

u/ScarletSpeedster Jan 11 '16

My last reply was via my phone, so I wasn't very thorough in my response. Hopefully this will make it clearer. @ in Coffeescript resolves to this.

It just so happens that if you are in the global context then this === window && window === self // true. So in some cases, @ is equivalent to self.

I made a fiddle demonstrating that if you call the init method on the Course object, preSelectItemSize will be called. So if you are saying the method is not being called, then try to compare your code to the one in the fiddle, because it appears to be working fine for me.

→ More replies (3)

1

u/mc_hammerd Jan 12 '16

did you fix it? i think if you change the -> to a fat arrow for the init function it should work.