File tree Expand file tree Collapse file tree 3 files changed +28
-10
lines changed
Expand file tree Collapse file tree 3 files changed +28
-10
lines changed Original file line number Diff line number Diff 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+
2837In action of your controller you put something like this:
2938
3039``` ruby
@@ -61,18 +70,27 @@ alert(gon.yourArray)
6170alert (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
6684Puts this line into ` Gemfile ` then run ` $ bundle ` :
6785
6886``` ruby
69- gem ' gon' , ' 1.1.1 '
87+ gem ' gon' , ' 1.1.2 '
7088```
7189
7290Or 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
7896Or manually install gon gem: ` $ gem install gon `
Original file line number Diff line number Diff 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>"
Original file line number Diff line number Diff line change 11module Gon
2- VERSION = '1.1.1 '
2+ VERSION = '1.1.2 '
33end
You can’t perform that action at this time.
0 commit comments