Skip to content

Commit 725e14d

Browse files
committed
Added need_tag option. Kudos to @afa
1 parent 2d6770a commit 725e14d

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 3.0.2
4+
5+
* Added need_tag option
6+
37
## 3.0.0
48

59
* Almost all code refactored

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ You can change the namespace of the variables:
8282
...
8383
```
8484

85+
You can get json without script tag (kudos to @afa):
86+
87+
``` erb
88+
<head>
89+
<title>some title</title>
90+
<script><%= include_gon(:need_tag => false) %></script>
91+
<!-- include your action js code with 'serverExports' namespace -->
92+
...
93+
```
94+
8595
You put something like this in the action of your controller:
8696

8797
``` ruby
@@ -329,13 +339,13 @@ Thats it!
329339
Puts this line into `Gemfile` then run `$ bundle`:
330340

331341
``` ruby
332-
gem 'gon', '3.0.0'
342+
gem 'gon', '3.0.2'
333343
```
334344

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

337347
``` ruby
338-
config.gem 'gon', :version => '3.0.0'
348+
config.gem 'gon', :version => '3.0.2'
339349
```
340350

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

lib/gon/base.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def render_data(options)
88
end
99
data = Gon.all_variables
1010
namespace = options[:namespace] || 'gon'
11-
start = '<script>window.' + namespace + ' = {};'
11+
need_tag = options[:need_tag].nil? || options[:need_tag]
12+
start = "#{need_tag ? '<script>' : ''}window.#{namespace} = {};"
1213
script = ''
1314

1415
if options[:camel_case]
@@ -21,7 +22,8 @@ def render_data(options)
2122
end
2223
end
2324

24-
script = start + Gon::Escaper.escape(script) + '</script>'
25+
script = start + Gon::Escaper.escape(script)
26+
script << '</script>' if need_tag
2527
script.html_safe
2628
end
2729

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 = '3.0.0'
2+
VERSION = '3.0.2'
33
end

spec/gon/basic_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@
6464
'</script>'
6565
end
6666

67+
it 'outputs correct js with an integer, camel-case and namespace' do
68+
Gon.int_cased = 1
69+
@base.include_gon(camel_case: true, namespace: 'camel_cased').should == \
70+
'<script>window.camel_cased = {};' +
71+
'camel_cased.intCased=1;' +
72+
'</script>'
73+
end
74+
75+
it 'outputs correct js with an integer and without tag' do
76+
Gon.int = 1
77+
@base.include_gon(need_tag: false).should == \
78+
'window.gon = {};' +
79+
'gon.int=1;'
80+
end
81+
6782
end
6883

6984
it 'returns exception if try to set public method as variable' do

0 commit comments

Comments
 (0)