Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Coffescript access functions from other assets?

Tags:

coffeescript

So I have two controllers, hotels and videos. I want the hotels.js.coffee to be able to access functions created in videos.js.coffee but I get a "is not defined" error.

I'm new to CoffeeScript so any clues would be appreciated.

like image 274
cbmeeks Avatar asked May 28 '11 16:05

cbmeeks


2 Answers

CoffeeScript will compile your coffee to JS wrapped in a self-executing function with the scope of the window (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.

like image 148
Thurloat Avatar answered Oct 17 '22 09:10

Thurloat


CoffeScript runs inside an anonymous function, so declared funcitons in the same file, aren't exported as global functions.

Try something like this to declare global functions:

window.myFunction = ->
    //some code
like image 20
erickzetta Avatar answered Oct 17 '22 09:10

erickzetta