1

我的 Rails 应用程序中有一个 Highcharts 图表,它有一个xaxis formatter用于获取数据的 javascript 回调函数:

spider_chart = LazyHighCharts::HighChart.new('graph') do |f|
  f.chart(polar: true, type: 'line', height: @height)
  f.xAxis(
    categories: categories.map{ |c| [@survey.id,c.id, c.name] },
      formatter: %|function() {return '<a href="http://localhost:3000/dashboards/spiders?level=2&survey_id=' + this.value[0] + '&category_id=' + this.value[1] + '">' + this.value[2] + '</a>';}|.js_code }
  )
  f.series(name: "Gemiddelde van huidige score", data: current_scores, pointPlacement: 'on')
  f.legend(enabled: @legend, align: 'center', verticalAlign: 'top', y: 10, layout: 'vertical')
end

当我使用硬编码的网址时,它可以工作:

formatter: %|function() {return '<a href="http://localhost:3000/dashboards/spiders?level=2&survey_id=' + this.value[0] + '&category_id=' + this.value[1] + '">' + this.value[2] + '</a>';}|.js_code }

当我在其中使用变量时,它不起作用。它没有给出错误,只是没有渲染图表:

formatter: %|function() {return '<a href="' + @environment + '/dashboards/spiders?level=2&survey_id=' + this.value[0] + '&category_id=' + this.value[1] + '">' + this.value[2] + '</a>';}|.js_code }

该变量@environment已正确填充http://localhost:3000. 当我使用window.location.hostnamethen 它也有效:

formatter: %|function() {return '<a href="https://' + window.location.hostname + '/dashboards/spiders?level=2&survey_id=' + this.value[0] + '&category_id=' + this.value[1] + '">' + this.value[2] + '</a>';}|.js_code }

不幸的是,这并不总是正确填写,所以我需要在我的控制器中有一个函数来确定正确的环境。

那么如何确保@environment可以使用该变量呢?

4

0 回答 0