如何将此数组传递给 html.erb 文件中的 javascript 函数,而不需要 erb 评估 Date.UTC?

发布于 2025-01-05 21:10:03 字数 3728 浏览 0 评论 0原文

我正在尝试将 HighStock Stockchart 配置为使用日期时间和值的二维数组。此方法仅在 Highcharts doco 中提到,而不是 highstock 的。

我的视图帮助程序文件中有一个方法:

  def byusers_chart_series(name, byusers)
          data = []
          codes_by_user = byusers.where(:hpmuser => "#{name}").group("date(hpmcreated)").select("date(hpmcreated) as dater, count(globalcode) as codes")
          codes_by_user.each do |record|
            #x = [Date.UTC("#{record.dater.year.inspect.to_i}", "#{record.dater.day.inspect.to_i}", "#{record.dater.month.inspect.to_i}"), "#{record.codes.inspect.to_i}"]
            x = [Date.UTC("#{record.dater.year.inspect.to_i}", "#{record.dater.day.inspect.to_i}", "#{record.dater.month.inspect.to_i}"), "#{record.codes.inspect.to_i}"]
            data << Array(x)
          end

          data
  end

这是返回的数组,与我希望它出现在 javascript 中的完全一样:

[[Date.UTC(2012, 1, 2), 1],
[Date.UTC(2012, 2, 2), 5],
[Date.UTC(2012, 3, 2), 15],
[Date.UTC(2012, 6, 2), 8],
[Date.UTC(2012, 7, 2), 3],
[Date.UTC(2012, 8, 2), 73],
[Date.UTC(2012, 9, 2), 77]]

这是 html.ERB 文件中的代码:

<script type="text/javascript" charset="UTF-8">

    $(function() {
        new Highcharts.StockChart({

            chart : {
                renderTo: 'weeks52dynamic',
                borderColor: '#EBBA95',
                borderWidth: 3
            },

            legend : {
                align : "left",
                enabled : true,
                layout : "vertical",
                verticalAlign : "top"
            },

            rangeSelector : {
                selected : 0
            },

            title : {
                text : 'title'
            },

            scrollbar: {
                barBackgroundColor: 'gray',
                barBorderRadius: 7,
                barBorderWidth: 0,
                buttonBackgroundColor: 'gray',
                buttonBorderWidth: 0,
                buttonBorderRadius: 7,
                trackBackgroundColor: 'none',
                trackBorderWidth: 1,
                trackBorderRadius: 8,
                trackBorderColor: '#CCC'
            },
            xAxis: {
                title: {
                    text: "Date"
                },

                type: "datetime",
                dateTimeLabelFormats: { // don't display the dummy year
            month: '%e. %b',
            year: '%b'
         }

            },
            yAxis: {
                min: 0,
                title: {
                    text: "Number of Codes"
                }


            },
            series: [
            <% { "New Codes" => Newcode.codesperday }.each do |name, newcodes| %>
            {
                name: "<%= name %>",
                 pointInterval: <%= 1.day * 1000 %>,
                 pointStart: <%= 350.weeks.ago.to_i * 1000 %>,
                data: <%= newcodes_chart_series(newcodes, 350.weeks.ago) %>,
                marker : {
                    enabled : true,
                    radius: 3
                }
            },
            <% end %>
            <% { "User" => Newcode.byuser }.each do |name, byusers| %>
            {
                name: "<%= name %>",
                data: <%= byusers_chart_series(name, byusers) %>,
                marker : {
                    enabled : true,
                    radius: 3
                }
            }
            <% end %>
            ]

        });

    });


</script>

我在浏览器中遇到的错误,我认为 ERB 模板正在尝试评估 Date.UTC 函数,而不是将其传递给 JavaScript 来执行。我的问题是,每当我在一个或多个数组中的 Data.UTC 周围放置任何类型的引号时,引号都会出现在发送到 javascript 的最终数组中。

如何传递精确的数组数组而不让 ruby​​ 对其进行评估?

I'm trying to configure the HighStock Stockchart to use a two dimensional array of datetime, and value. This method is only mentioned in the Highcharts doco, not the highstock's.

I have a method in my view helper file:

  def byusers_chart_series(name, byusers)
          data = []
          codes_by_user = byusers.where(:hpmuser => "#{name}").group("date(hpmcreated)").select("date(hpmcreated) as dater, count(globalcode) as codes")
          codes_by_user.each do |record|
            #x = [Date.UTC("#{record.dater.year.inspect.to_i}", "#{record.dater.day.inspect.to_i}", "#{record.dater.month.inspect.to_i}"), "#{record.codes.inspect.to_i}"]
            x = [Date.UTC("#{record.dater.year.inspect.to_i}", "#{record.dater.day.inspect.to_i}", "#{record.dater.month.inspect.to_i}"), "#{record.codes.inspect.to_i}"]
            data << Array(x)
          end

          data
  end

This is the array that is returned, exactly as i want it to appear in the javascript:

[[Date.UTC(2012, 1, 2), 1],
[Date.UTC(2012, 2, 2), 5],
[Date.UTC(2012, 3, 2), 15],
[Date.UTC(2012, 6, 2), 8],
[Date.UTC(2012, 7, 2), 3],
[Date.UTC(2012, 8, 2), 73],
[Date.UTC(2012, 9, 2), 77]]

And this is the code from the html.ERB file:

<script type="text/javascript" charset="UTF-8">

    $(function() {
        new Highcharts.StockChart({

            chart : {
                renderTo: 'weeks52dynamic',
                borderColor: '#EBBA95',
                borderWidth: 3
            },

            legend : {
                align : "left",
                enabled : true,
                layout : "vertical",
                verticalAlign : "top"
            },

            rangeSelector : {
                selected : 0
            },

            title : {
                text : 'title'
            },

            scrollbar: {
                barBackgroundColor: 'gray',
                barBorderRadius: 7,
                barBorderWidth: 0,
                buttonBackgroundColor: 'gray',
                buttonBorderWidth: 0,
                buttonBorderRadius: 7,
                trackBackgroundColor: 'none',
                trackBorderWidth: 1,
                trackBorderRadius: 8,
                trackBorderColor: '#CCC'
            },
            xAxis: {
                title: {
                    text: "Date"
                },

                type: "datetime",
                dateTimeLabelFormats: { // don't display the dummy year
            month: '%e. %b',
            year: '%b'
         }

            },
            yAxis: {
                min: 0,
                title: {
                    text: "Number of Codes"
                }


            },
            series: [
            <% { "New Codes" => Newcode.codesperday }.each do |name, newcodes| %>
            {
                name: "<%= name %>",
                 pointInterval: <%= 1.day * 1000 %>,
                 pointStart: <%= 350.weeks.ago.to_i * 1000 %>,
                data: <%= newcodes_chart_series(newcodes, 350.weeks.ago) %>,
                marker : {
                    enabled : true,
                    radius: 3
                }
            },
            <% end %>
            <% { "User" => Newcode.byuser }.each do |name, byusers| %>
            {
                name: "<%= name %>",
                data: <%= byusers_chart_series(name, byusers) %>,
                marker : {
                    enabled : true,
                    radius: 3
                }
            }
            <% end %>
            ]

        });

    });


</script>

The error I get in my browser, I think the ERB template is trying to evaluate the Date.UTC function, rather than passing it to JavaScript for it to do. My problem is whenever I put any sort of quotes around the Data.UTC in the array or arrays, the quotes appear in the final array of arrays sent to javascript.

How can I pass that exact array of arrays without having ruby evaluate it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2025-01-12 21:10:03

在我们修复它的同时,让您的方法变得更简单。如果您发现自己有这样的代码:

foo = []
bar.each{ … foo << jim }

...那么您应该使用以下代码:

foo = bar.map{ … jim }

您的 Ruby 代码中有 Date.UTC,这需要 Ruby 来运行此代码在构建阵列时。我们不会创建数组的数组,而是使用 < code>Enumerable#map 将您的 codes_by_user 转换为 字符串 数组,这些字符串是您想要的 JS 代码:

def byusers_chart_series(name, byusers)
  codes_by_user = byusers.where(:hpmuser => "#{name}").group("date(hpmcreated)").select("date(hpmcreated) as dater, count(globalcode) as codes")
  codes_by_user.map do |record|
    parts = %w[year month day].map{ |s| record.dater.send(s) }
    "[Date.UTC(#{parts.join(',')}),#{record.codes}]"
  end
end

然后在您的视图中:

data: [ <%= byusers_chart_series(name, byusers).join(", ") %> ],

这将产生在 HTML 中输出如下所示:

data: [ [Date.UTC(2011,10,7),42], [Date.UTC(2012,4,3),17] ],

Let's make your method simpler while we're fixing it. If you ever find yourself with code like this:

foo = []
bar.each{ … foo << jim }

…then instead you should be using this:

foo = bar.map{ … jim }

You have Date.UTC in your Ruby code, which requires Ruby to run this code while building the array. Instead of creating an array of arrays, we'll use Enumerable#map to convert your codes_by_user into an array of strings that are the JS code that you want:

def byusers_chart_series(name, byusers)
  codes_by_user = byusers.where(:hpmuser => "#{name}").group("date(hpmcreated)").select("date(hpmcreated) as dater, count(globalcode) as codes")
  codes_by_user.map do |record|
    parts = %w[year month day].map{ |s| record.dater.send(s) }
    "[Date.UTC(#{parts.join(',')}),#{record.codes}]"
  end
end

And then in your view:

data: [ <%= byusers_chart_series(name, byusers).join(", ") %> ],

This will produce output like the following in your HTML:

data: [ [Date.UTC(2011,10,7),42], [Date.UTC(2012,4,3),17] ],
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文