3

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

asked Oct 13, 2011 at 20:03

2 Answers 2

16

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 
answered Jan 4, 2012 at 15:55
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Oct 13, 2011 at 20:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.