lazy_high_charts and foundation conflict loading javascript

408 views Asked by At

I'm using lazy_high_charts and foundation 4.

In my view I have <%= high_chart("my_id", @chart) %>, which generates the following:

<section>
  <script type="text/javascript">
    (function() {
      var onload = window.onload;                      # I think
      window.onload = function(){                      # the issue
        if (typeof onload == "function") onload();     # is here
          var options = { "title": { "text": "Combination chart" },"legend": { "layout": "vertical","style": {  } },"xAxis": {  },"yAxis": { "title": { "text": null },"labels": {  } },"tooltip": { "enabled": true },"credits": { "enabled": false },"plotOptions": { "areaspline": {  } },"chart": { "defaultSeriesType": "line","renderTo": "my_id" },"subtitle": {  },"series": [{ "type": "spline","name": "Average","data": [ 3,2.67,3,6.33,3.33 ] }] };

    window.chart_my_id = new Highcharts.Chart(options);

      };
    })()
  </script>
<div id="my_id"></div>
</section>

However, it is not displaying because of the following lines generated by foundation 4 in my application.js

$(document).foundation();
$(function(){ $(document).foundation(); });

If I delete those lines the chart loads.

How can I use foundation and lazy_high_charts together?

2

There are 2 answers

0
gabrielhilal On BEST ANSWER

I solved the problem updating the zurb-foundation and lazy_high_charts gems:

In my Gemfile:

gem 'zurb-foundation', '~> 4.0.0'
gem 'lazy_high_charts'

Then:

bundle install
bundle update lazy_high_charts
rails g foundation:install

I have also included the following lines in the application.html.erb:

  ...
  <%= javascript_include_tag "vendor/custom.modernizr" %>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  ...

After that, the <%= high_chart("my_id", @chart) %> code is generating the following javascript:

<script type="text/javascript">        
(function() {
  var f = function(){
  document.removeEventListener('page:load', f, true);
...

I hope it helps people facing the same problem.

3
mhsekhavat On

instead of

(function() {
var onload = window.onload;                      # I think
window.onload = function(){                      # the issue

write

$(function(){

The resulting script tag will look:

<section>
  <script type="text/javascript">
        $(function() {
          var options = { "title": { "text": "Combination chart" },"legend": { "layout": "vertical","style": {  } },"xAxis": {  },"yAxis": { "title": { "text": null },"labels": {  } },"tooltip": { "enabled": true },"credits": { "enabled": false },"plotOptions": { "areaspline": {  } },"chart": { "defaultSeriesType": "line","renderTo": "my_id" },"subtitle": {  },"series": [{ "type": "spline","name": "Average","data": [ 3,2.67,3,6.33,3.33 ] }] };

    window.chart_my_id = new Highcharts.Chart(options);

    });
  </script>
<div id="my_id"></div>
</section>

Make sure you have put jquery script before it.