Reputation: 25542
I have a CoffeeScript file in which I am writing a class for interactions with an audio player but for some reason I can't get it to play nicely inside of another coffeeScript file in my Rails app.
player.coffee:
window.Player = ->
constructor: (@player_id = "player") ->
jw = {
'flashplayer':"<%= asset_path('player.swf') %>"
'controlbar': 'top'
'autostart': 'false'
'width':'400'
'height':'49'
'playlist': '[]'
'skin':"<%= asset_path('awardfm.zip') %>"
'dock':'false'
'wmode':'transparent'
}
jwplayer(@player_id).setup(jw);
play: (track_data) ->
console.log track_data
player_interactions.coffee
$ ->
jw = window.Player "player" || {}
$('.play').click ->
jw.play('test')
I keep getting this error:
Uncaught ReferenceError: Player is not defined
Its now working with the above code samples
Upvotes: 3
Views: 3092
Reputation: 2809
To make a class globally addressable you should prefix the name of the class with "@" (unless you are within a closure in which case you need to prefix it with "window." but you probably wouldn't want to do that anyway).
player.coffee
class @Player
constructor: (@player = "player") ->
...
play: (track_data) ->
...
player_interactions.coffee
jw = new Player
Upvotes: 16
Reputation: 25542
To access a function from one coffeescript file in another, attach the function in the top level window object then reference it in your file, window.MyClass
Upvotes: 2