grails - functions in coffee-file are not available from other js -


i try use coffeescript in grails-project. achive decided use coffeescript-resources plugin. compiled coffee in result view looks follows:

(function() {     var somefunc;     somefunc = function() {        return alert("hello");     }; }).call(this);  

and in case cant call it. have not found proper configurations in plugin documentation avoid using anonymous functions while compiling coffee-file. how can solve this?

from fine manual:

lexical scoping , variable safety
[...]
although suppressed within documentation clarity, coffeescript output wrapped in anonymous function: (function(){ ... })(); safety wrapper, combined automatic generation of var keyword, make exceedingly difficult pollute global namespace accident.

if you'd create top-level variables other scripts use, attach them properties on window, or on exports object in commonjs. existential operator (covered below), gives reliable way figure out add them; if you're targeting both commonjs , browser: exports ? this

so self-invoking function wrapper exists prevent polluting global namespace. if want put global namespace have put there explicitly; in browser, can using:

window.somefunc = -> alert('hello') 

or

@somefunc = -> alert('hello') 

the @somefunc form assumes you're @ top of scope (i.e. not inside function or class).

alternatively, find way compile coffeescript --bare:

-b, --bare
compile javascript without top-level function safety wrapper.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -