How can I use access functions from other Coffescript assets?

So, I have two controllers, hotels and videos . I want hotels.js.coffee to hotels.js.coffee able to access the functions created in videos.js.coffee , but I get an "undefined" error.

I am new to CoffeeScript, so any hints would be appreciated.

+4
coffeescript
May 28 '11 at 16:53
source share
4 answers

CoffeeScript will compile your coffee in JS wrapped in a self-executing function with a window scope (function {}). call (this);

So in videos.js.coffee you can write something like:

  @getVideo: (id) -> 

and the getVideo function will be bound to the window object.

+13
Jun 07 2018-11-11T00:
source share

CoffeScript works inside an anonymous function, so declared functions in the same file are not exported as global functions.

Try something like this to declare global functions:

 window.myFunction = -> //some code 
+7
May 28 '11 at 17:01
source share

At compile time, CoffeeScript transfers your code to an anonymous function and applies it. You must export your public interface in the expected manner for your environment.

 (exports || window).publicMethod = (foo, bar) -> foo + bar 

Then you need to use require() in node.js and by referencing the window object in the browser.

There are other ways to do this in the browser. Take a look at RequireJS .

+3
May 28 '11 at 17:01
source share

Indeed, you can use either the top window variable or the export object provided through CommonJS. Please note: you can also provide access to the full controller, not just functions.

See Sections "Lexical Scope and Variable Security" and "Text / Coffee Shop" Script Tags: http://jashkenas.github.com/coffee-script/ .

0
Jun 01 '11 at 19:20
source share



All Articles