lazy_high_charts和基础冲突加载javascript

时间:2013-08-22 17:39:03

标签: javascript jquery ruby-on-rails zurb-foundation lazy-high-charts

我正在使用lazy_high_chartsfoundation 4

在我看来,我有<%= high_chart("my_id", @chart) %>,它会生成以下内容:

<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>

但是,由于foundation 4

application.js生成了以下行,因此无法显示
$(document).foundation();
$(function(){ $(document).foundation(); });

如果删除这些行,则会加载图表。

如何一起使用foundationlazy_high_charts

2 个答案:

答案 0 :(得分:1)

而不是

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

$(function(){

生成的脚本标记将显示:

<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>

确保您已将jquery脚本放在它之前。

答案 1 :(得分:1)

我解决了更新zurb-foundationlazy_high_charts宝石的问题:

在我的Gemfile中:

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

然后:

bundle install
bundle update lazy_high_charts
rails g foundation:install

我还在application.html.erb中包含以下几行:

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

之后,<%= high_chart("my_id", @chart) %>代码生成以下javascript:

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

我希望它可以帮助人们面对同样的问题。