Skip to content

Support generating chart Javascript without <script> tag #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/google_visualr/app/helpers/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module ViewHelper
helper_method "render_chart"
end

def render_chart(chart, dom)
chart.to_js(dom).html_safe
def render_chart(chart, dom, options = {})
chart.to_js(dom, options).html_safe
end

end
Expand Down
7 changes: 4 additions & 3 deletions lib/google_visualr/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def add_listener(event, callback)
# Parameters:
# *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.

def to_js(element_id)
js = "\n<script type='text/javascript'>"
def to_js(element_id, options = {})
js = ""
js << "\n<script type='text/javascript'>" unless options[:script_tag] == false
js << "\n google.load('visualization','1', {packages: ['#{package_name}'], callback: #{chart_function_name(element_id)}});"
js << "\n function #{chart_function_name(element_id)}() {"
js << "\n #{@data_table.to_js}"
Expand All @@ -48,7 +49,7 @@ def to_js(element_id)
end
js << "\n chart.draw(data_table, #{js_parameters(@options)});"
js << "\n };"
js << "\n</script>"
js << "\n</script>" unless options[:script_tag] == false
js
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/google_visualr/base_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
it "generates JS" do
js = @chart.to_js("body")
js.should == base_chart_js("body")
js.should include("<script")
end

it "generates JS without script tag" do
js = @chart.to_js("body", :script_tag => false)
js.should_not include("<script")
end

it "generates JS with listeners" do
Expand Down