Javascript Variables in Rails Assets

171 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 answers

0
Prakash On

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

use window.classie instead of var classie

2
Leonel Galán 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.