Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added custom namespacing for js gon

  • Loading branch information...
commit d55978ff5ea4a7ad7697866520d4acebd2824a7b 1 parent dd4e7e3
Alexey Gaziev authored
Showing with 28 additions and 10 deletions.
  1. +21 −3 README.md
  2. +6 −6 lib/gon/helpers.rb
  3. +1 −1  lib/gon/version.rb
24 README.md
View
@@ -20,11 +20,20 @@ For camelize your variables in js you can use:
<head>
<title>some title</title>
<%= include_gon(:camel_case => true) %>
- <!-- or just include_gon(true) -->
<!-- include your action js code with camelized variables -->
...
```
+For different namespace of your variables in js you can use:
+
+``` erb
+<head>
+ <title>some title</title>
+ <%= include_gon(:namespace => 'serverExports') %>
+ <!-- include your action js code with 'serverExports' namespace -->
+ ...
+```
+
In action of your controller you put something like this:
``` ruby
@@ -61,18 +70,27 @@ alert(gon.yourArray)
alert(gon.yourHash)
```
+With custom namespace and camelize:
+
+``` js
+alert(customNamespace.yourInt)
+alert(customNamespace.yourOtherInt)
+alert(customNamespace.yourArray)
+alert(customNamespace.yourHash)
+```
+
## Installation
Puts this line into `Gemfile` then run `$ bundle`:
``` ruby
-gem 'gon', '1.1.1'
+gem 'gon', '1.1.2'
```
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
``` ruby
-config.gem 'gon', :version => '1.1.1'
+config.gem 'gon', :version => '1.1.2'
```
Or manually install gon gem: `$ gem install gon`
12 lib/gon/helpers.rb
View
@@ -5,18 +5,18 @@ def self.included base
end
module InstanceMethods
- def include_gon(camel_case = false)
+ def include_gon(options = {})
if Gon.request_env
data = Gon.all_variables
-
- script = "<script>window.gon = {};"
- unless camel_case
+ namespace = options[:namespace] || 'gon'
+ script = "<script>window." + namespace + " = {};"
+ unless options[:camel_case]
data.each do |key, val|
- script += "gon." + key.to_s + '=' + val.to_json + ";"
+ script += namespace + "." + key.to_s + '=' + val.to_json + ";"
end
else
data.each do |key, val|
- script += "gon." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
+ script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
end
end
script += "</script>"
2  lib/gon/version.rb
View
@@ -1,3 +1,3 @@
module Gon
- VERSION = '1.1.1'
+ VERSION = '1.1.2'
end
Please sign in to comment.
Something went wrong with that request. Please try again.