I have two files that use some common javascript code and the rails gon gem.
So for file 1: file1.js
looks like this
$(function () {
function a() {
....
}
$('id').on('click', function() {
c = gon.obj1
c.doSomething
....
})
function otherMethods(){
}
})
For file 2: file2.js.erb
I have,
var c = abc
function a() {
....
}
$('id').on('click', function() {
c.doSomething
....
})
//other code
Now I am considering making my code DRY by extracting the common code in file3.js
:
function a() {
....
}
$('id').on('click', function() {
c.doSomething
....
})
Since I am new to javascript, I am not sure about how I could populate c such that both file1.js
and file2.js.erb
to work correctly.
- How do I pass var c as an argument to the common file from file1 and file2, to the common code (let's call it
file3.js
) - Do I just do something like
<%= javascript_include_tag "file3.js" %>
infile2
, and similarly infile1
to access the common code?
Eventually I want to have file1.js
to look like:
$(function () {
function otherMethods(){
}
})
and file2.js.erb
to look like
//other code