Permalink
Browse files

Added need_tag option. Kudos to @afa

  • Loading branch information...
1 parent 2d6770a commit 725e14dd22008a80dc6da059dddd0cf948b0d17c @gazay committed Apr 28, 2012
Showing with 36 additions and 5 deletions.
  1. +4 −0 CHANGELOG.md
  2. +12 −2 README.md
  3. +4 −2 lib/gon/base.rb
  4. +1 −1 lib/gon/version.rb
  5. +15 −0 spec/gon/basic_spec.rb
View
4 CHANGELOG.md
@@ -1,5 +1,9 @@
# CHANGELOG
+## 3.0.2
+
+ * Added need_tag option
+
## 3.0.0
* Almost all code refactored
View
14 README.md
@@ -82,6 +82,16 @@ You can change the namespace of the variables:
...
```
+You can get json without script tag (kudos to @afa):
+
+``` erb
+<head>
+ <title>some title</title>
+ <script><%= include_gon(:need_tag => false) %></script>
+ <!-- include your action js code with 'serverExports' namespace -->
+ ...
+```
+
You put something like this in the action of your controller:
``` ruby
@@ -329,13 +339,13 @@ Thats it!
Puts this line into `Gemfile` then run `$ bundle`:
``` ruby
-gem 'gon', '3.0.0'
+gem 'gon', '3.0.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 => '3.0.0'
+config.gem 'gon', :version => '3.0.2'
```
Or manually install gon gem: `$ gem install gon`
View
6 lib/gon/base.rb
@@ -8,7 +8,8 @@ def render_data(options)
end
data = Gon.all_variables
namespace = options[:namespace] || 'gon'
- start = '<script>window.' + namespace + ' = {};'
+ need_tag = options[:need_tag].nil? || options[:need_tag]
+ start = "#{need_tag ? '<script>' : ''}window.#{namespace} = {};"
script = ''
if options[:camel_case]
@@ -21,7 +22,8 @@ def render_data(options)
end
end
- script = start + Gon::Escaper.escape(script) + '</script>'
+ script = start + Gon::Escaper.escape(script)
+ script << '</script>' if need_tag
script.html_safe
end
View
2 lib/gon/version.rb
@@ -1,3 +1,3 @@
module Gon
- VERSION = '3.0.0'
+ VERSION = '3.0.2'
end
View
15 spec/gon/basic_spec.rb
@@ -64,6 +64,21 @@
'</script>'
end
+ it 'outputs correct js with an integer, camel-case and namespace' do
+ Gon.int_cased = 1
+ @base.include_gon(camel_case: true, namespace: 'camel_cased').should == \
+ '<script>window.camel_cased = {};' +
+ 'camel_cased.intCased=1;' +
+ '</script>'
+ end
+
+ it 'outputs correct js with an integer and without tag' do
+ Gon.int = 1
+ @base.include_gon(need_tag: false).should == \
+ 'window.gon = {};' +
+ 'gon.int=1;'
+ end
+
end
it 'returns exception if try to set public method as variable' do

0 comments on commit 725e14d

Please sign in to comment.