Skip to content

Commit dd4e7e3

Browse files
committed
Now you can use camelized variables in your JS
1 parent f1293d1 commit dd4e7e3

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ If you need to send some data to your js files and you don't want to do this wit
1414
...
1515
```
1616

17+
For camelize your variables in js you can use:
18+
19+
``` erb
20+
<head>
21+
<title>some title</title>
22+
<%= include_gon(:camel_case => true) %>
23+
<!-- or just include_gon(true) -->
24+
<!-- include your action js code with camelized variables -->
25+
...
26+
```
27+
1728
In action of your controller you put something like this:
1829

1930
``` ruby
@@ -41,18 +52,27 @@ alert(gon.your_array)
4152
alert(gon.your_hash)
4253
```
4354

55+
With camelize:
56+
57+
``` js
58+
alert(gon.yourInt)
59+
alert(gon.yourOtherInt)
60+
alert(gon.yourArray)
61+
alert(gon.yourHash)
62+
```
63+
4464
## Installation
4565

4666
Puts this line into `Gemfile` then run `$ bundle`:
4767

4868
``` ruby
49-
gem 'gon', '1.1.0'
69+
gem 'gon', '1.1.1'
5070
```
5171

5272
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
5373

5474
``` ruby
55-
config.gem 'gon', :version => '1.1.0'
75+
config.gem 'gon', :version => '1.1.1'
5676
```
5777

5878
Or manually install gon gem: `$ gem install gon`

lib/gon/helpers.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ def self.included base
55
end
66

77
module InstanceMethods
8-
def include_gon
8+
def include_gon(camel_case = false)
99
if Gon.request_env
1010
data = Gon.all_variables
1111

1212
script = "<script>window.gon = {};"
13-
data.each do |key, val|
14-
script += "gon." + key.to_s + '=' + val.to_json + ";"
13+
unless camel_case
14+
data.each do |key, val|
15+
script += "gon." + key.to_s + '=' + val.to_json + ";"
16+
end
17+
else
18+
data.each do |key, val|
19+
script += "gon." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
20+
end
1521
end
1622
script += "</script>"
1723
script.html_safe

lib/gon/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Gon
2-
VERSION = '1.1.0'
2+
VERSION = '1.1.1'
33
end

0 commit comments

Comments
 (0)