1

我有这个玉模板:

html
  head
    script(type="text/javascript" src="https://www.google.com/jsapi")
    script(type='text/javascript')
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
      var data = google.visualization.arrayToDataTable([
      ['Date', 'Tin_A' ],

       - each datapoint in myData
            "[" + datapoint.date + "," + datapoint.value + "],"

      ]);
      var options = {
      title: 'bla'
      };
      var chart = new google.visualization.LineChart(document.getElementById('chart_line'));
      chart.draw(data, options);
      }


  body
    h1= title
    #chart_line

我正在使用此调用在 express/node.js 中呈现翡翠模板:

app.get('/', function(req, res){
  sensors.findSensorAllData(2, 2, function (error,emps){
        console.log(emps);
        res.render('tmp', {
            title : 'Temperatures in a row',
            myData : emps
        });
    });
});

的输出console.log(emps)是:

[ { _id: 524b8642028e167fb0e3661d,
    sensor_id: 2,
    value: 49,
    date: Tue Oct 01 2013 20:34:40 GMT-0600 (CST) },
  { _id: 524b863d028e167fb0e3661c,
    sensor_id: 2,
    value: 19,
    date: Tue Oct 01 2013 20:34:35 GMT-0600 (CST) } ]

在渲染发生后,我希望在玉模板的 javascrip 中有值......但它不起作用。我在纯文本中只得到相同的行,好像该行- each datapoing in myData没有意义......我做错了什么?谢谢

--- 编辑:如果我更换线路,一切正常

- each datapoint in myData
            "[" + datapoint.date + "," + datapoint.value + "],"

  ['2004',  20],
  ['2005',  30],
  ['2006',  40]
4

3 回答 3

1

丑陋的警报:

script
  ...
  var data = google.visualization.arrayToDataTable(['Date', 'Tin_A' ].concat(!{JSON.stringify(myData.map(function(i) { return [ i.date, i.value ] })) }));
于 2013-10-02T07:32:07.403 回答
1

请参阅此问题的选择答案,了解为什么您尝试做的事情不起作用。(JADE + EXPRESS:迭代内联 JS 代码中的对象(客户端)?

基本上,只要你点击脚本标签,你就告诉 Jade 解析器以原始形式处理事物,并且不会进行进一步的处理。您真正想要做的是为您的代码重做脚本标记,如下所示:

- if (typeof(pins) != "object")
!= "<script type='text/javascript'>"
!=   "google.load('visualization', '1', {packages:['corechart']});
!=   "google.setOnLoadCallback(drawChart);
!=   "function drawChart() {
!=   "var data = google.visualization.arrayToDataTable([
!=   "['Date', 'Tin_A' ],"
- forEach datapoint in myData
  !=   "[" + datapoint.date + "," + datapoint.value + "],"

!=   "]);"
!=   "var options = {"
!=   "title: 'bla'"
!=   "};"
!=   "var chart = new google.visualization.LineChart(document.getElementById('chart_line'));"
!=   "chart.draw(data, options);"
!=   "}"

试试这个,但我相当肯定它应该工作。

PS:上面的链接也(我相信)清楚地说明了为什么前面的答案应该是不正确的,因为你不能在 Jade 脚本标签中使用那种模板占位符插值。

于 2013-10-02T03:49:53.763 回答
1

我认为您可能会因为括号中的引号而意外注入 String 而不是 Array:

- each datapoint in myData
        "[" + datapoint.date + "," + datapoint.value + "],"

我对 Jade 不是很熟悉,但我认为您可能想要执行以下操作:

- each datapoint in myData
        [#{datapoint.date}, #{datapoint.value}],

此外,在您提供的示例数据中,您仅使用日期的年份部分,但datapoint.date属性的内容可能是完整的日期对象,我不确定这是否是您想要的。

于 2013-10-02T03:42:39.150 回答