Skip to content

Commit d55978f

Browse files
committed
Added custom namespacing for js gon
1 parent dd4e7e3 commit d55978f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ For camelize your variables in js you can use:
2020
<head>
2121
<title>some title</title>
2222
<%= include_gon(:camel_case => true) %>
23-
<!-- or just include_gon(true) -->
2423
<!-- include your action js code with camelized variables -->
2524
...
2625
```
2726

27+
For different namespace of your variables in js you can use:
28+
29+
``` erb
30+
<head>
31+
<title>some title</title>
32+
<%= include_gon(:namespace => 'serverExports') %>
33+
<!-- include your action js code with 'serverExports' namespace -->
34+
...
35+
```
36+
2837
In action of your controller you put something like this:
2938

3039
``` ruby
@@ -61,18 +70,27 @@ alert(gon.yourArray)
6170
alert(gon.yourHash)
6271
```
6372

73+
With custom namespace and camelize:
74+
75+
``` js
76+
alert(customNamespace.yourInt)
77+
alert(customNamespace.yourOtherInt)
78+
alert(customNamespace.yourArray)
79+
alert(customNamespace.yourHash)
80+
```
81+
6482
## Installation
6583

6684
Puts this line into `Gemfile` then run `$ bundle`:
6785

6886
``` ruby
69-
gem 'gon', '1.1.1'
87+
gem 'gon', '1.1.2'
7088
```
7189

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

7492
``` ruby
75-
config.gem 'gon', :version => '1.1.1'
93+
config.gem 'gon', :version => '1.1.2'
7694
```
7795

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

lib/gon/helpers.rb

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

77
module InstanceMethods
8-
def include_gon(camel_case = false)
8+
def include_gon(options = {})
99
if Gon.request_env
1010
data = Gon.all_variables
11-
12-
script = "<script>window.gon = {};"
13-
unless camel_case
11+
namespace = options[:namespace] || 'gon'
12+
script = "<script>window." + namespace + " = {};"
13+
unless options[:camel_case]
1414
data.each do |key, val|
15-
script += "gon." + key.to_s + '=' + val.to_json + ";"
15+
script += namespace + "." + key.to_s + '=' + val.to_json + ";"
1616
end
1717
else
1818
data.each do |key, val|
19-
script += "gon." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
19+
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
2020
end
2121
end
2222
script += "</script>"

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.1'
2+
VERSION = '1.1.2'
33
end

0 commit comments

Comments
 (0)