Skip to content

Commit 9d34903

Browse files
committed
added type text/javascript option
1 parent 25fe321 commit 9d34903

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ You can initialize window.gon = {}; on each request
9292
...
9393
```
9494

95+
You can initialize script tag with type="text/javascript"
96+
97+
``` erb
98+
<head>
99+
<title>some title</title>
100+
<%= include_gon(:need_type => true) %>
101+
<!-- include your action js code with 'serverExports' namespace -->
102+
...
103+
```
104+
95105
You can get json without script tag (kudos to @afa):
96106

97107
``` erb

lib/gon/base.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ def render_data(options)
77
if Gon.global.all_variables.present?
88
data[:global] = Gon.global.all_variables
99
end
10-
namespace = options[:namespace] || 'gon'
11-
need_tag = options[:need_tag].nil? || options[:need_tag]
12-
start = "#{need_tag ? '<script>' : ''}window.#{namespace} = {};"
13-
script = ''
10+
namespace = options[:namespace] || 'gon'
11+
need_tag = options[:need_tag].nil? || options[:need_tag]
12+
13+
need_type = !options[:need_type].nil? || options[:need_type]
14+
need_type ? type ='<script type="text/javascript">' : type = '<script>'
15+
16+
start = "#{need_tag ? type : ''}window.#{namespace} = {};"
17+
script = ''
1418

1519
if data.present?
1620
if options[:camel_case]

spec/gon/basic_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
it 'supports all data types' do
2020
Gon.clear
21-
Gon.int = 1
22-
Gon.float = 1.1
23-
Gon.string = 'string'
24-
Gon.array = [ 1, 'string' ]
25-
Gon.hash_var = { :a => 1, :b => '2'}
26-
Gon.hash_w_array = { :a => [ 2, 3 ] }
27-
Gon.klass = Hash
21+
Gon.int = 1
22+
Gon.float = 1.1
23+
Gon.string = 'string'
24+
Gon.array = [ 1, 'string' ]
25+
Gon.hash_var = { :a => 1, :b => '2'}
26+
Gon.hash_w_array = { :a => [ 2, 3 ] }
27+
Gon.klass = Hash
2828
end
2929

3030
end
@@ -79,19 +79,25 @@
7979
'gon.int=1;'
8080
end
8181

82-
8382
it 'outputs correct js without variables, without tag and gon init' do
8483
@base.include_gon(need_tag: false, init: true).should == \
8584
'window.gon = {};'
8685
end
8786

88-
it 'outputs correct js without variables, without tag and gon init' do
87+
it 'outputs correct js without variables, without tag, gon init and an integer' do
8988
Gon.int = 1
9089
@base.include_gon(need_tag: false, init: true).should == \
9190
'window.gon = {};' +
9291
'gon.int=1;'
9392
end
9493

94+
it 'outputs correct js with type text/javascript' do
95+
@base.include_gon(need_type: true, init: true).should == \
96+
'<script type="text/javascript">' +
97+
'window.gon = {};'\
98+
'</script>'
99+
end
100+
95101
end
96102

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

0 commit comments

Comments
 (0)