Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not encapsulate Coffeescript

I don't know whether all coffeescript compilers wrap their scripts in anonymous functions, but that's what I see Rails doing. How can I disable this encapsulation?

I want to put several initializing functions in a single coffeescript file, then call one of them from an on-page <script> tag (so that each page calls a different initializer). This can't be if the initializing functions are encapsulated.

Coffeescript initializer functions:

initializerA = -> console.log 'foo'
initializerB = -> console.log 'bar'

On-page code:

<script>$(document).ready(initializerA)</script>

Sys: coffee-rails 3.2.1, Rails 3.2.3, Ruby 1.9.3

like image 834
JellicleCat Avatar asked Jan 17 '23 10:01

JellicleCat


1 Answers

Coffeescript documentation says that all script will be wrapped in an anonymous function for the sake of encapsulation/safety. To make something accessible within the global scope do the following:

window.myvar = myvar
like image 82
user1454227 Avatar answered Jan 24 '23 23:01

user1454227