Javascript Variables in Rails Assets

191 Views Asked by At

I have a Rails 4 app. I define a variable var a = 1 in app/assets/javascripts/a.js. Then I'd like to use that variable in app/assets/javascripts/b.js, but whenever I use a in b.js, it says it's undefined. Why? Thanks.

2

There are 2 best solutions below

0
On

as @leito said your variable is local one and needs to be declared global

use window.classie instead of var classie

2
On

Make sure a.js is required before b.js:

//= require a
//= require b

If using require_tree . files will be required in alphabetical order (which works in your example: a, b)

EDIT: Didn't notice before, but @ConnorCMcKee answered in a comment before me.