1

I have shared.js.coffee

Shared =

  shared_method: (msg) ->
    alert(msg)

And I have test.js.coffee

Shared.shared_method("this doesn't work")

And I load the shared code first in application.js as described https://stackoverflow.com/a/20563242/656510

 //= require ./shared 
 //= require_tree .

However it does not work in development, i get: Uncaught ReferenceError: Shared is not defined

When i look at the JS it is being served as separate files rather than being compiled into application.js as it would in production.

As i thought it could be to do with Asset Pipeline configuration I created a clean Rails 4.1 app which demonstrates the problem.

https://github.com/itinsley/asset_pipeline_weirdness

Help greatly appreciated.

Community
  • 1
  • 1
Ian
  • 315
  • 1
  • 6
  • 13

1 Answers1

1

You need to use a global namespace:

 @Shared =

  shared_method: (msg) ->
    alert(msg)

If you want you can use a namespace for your app:

 @myApp ||= {}

 myApp.Shared =

  shared_method: (msg) ->
    alert(msg)
sites
  • 21,417
  • 17
  • 87
  • 146