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
2 Answers 2
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
Comments
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
Comments
Explore related questions
See similar questions with these tags.