“尝试在清除的范围内运行编译和运行脚本” - 使用 Google Graph API 在 Drupal 中出错

"attempt to run compile-and-go script on a cleared scope" - Error in Drupal with Google Graph api

本文关键字:运行 Google 使用 Graph API 出错 Drupal 清除 范围内 编译 脚本      更新时间:2023-09-26

我正在尝试将Google graph api包含在我的Drupal 7模块(不是Google Graph模块)中。但是当我尝试调用服务时,我得到:

  • "尝试在清除的作用域上运行编译和运行脚本" - 错误火狐。
  • "无法发送响应:无法按chrome.extension.onRequest listener per document" -error in Chrome.

代码如下:

(function ($) {
  Drupal.behaviors.exampleModule = {
    attach: function (context, settings) {
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', {'packages':['corechart']});
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);
        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.
        function drawChart() {
          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'Slices');
          data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1],
            ['Zucchini', 1],
            ['Pepperoni', 2]
          ]);
          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':400,
                         'height':300};
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        alert("test");
    }
  };
})(jQuery);

我能做些什么来让它工作?-谢谢

我修复了它:

google.load('visualization', '1.0', {'packages':['corechart']});行应该先调用,紧接着(function ($) {。 所以解决方案是:

(function ($) {
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1.0', {'packages':['corechart']});
  Drupal.behaviors.exampleModule = {
    attach: function (context, settings) {
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);
        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.
        function drawChart() {
          // Create the data table.
          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Topping');
          data.addColumn('number', 'Slices');
          data.addRows([
            ['Mushrooms', 3],
            ['Onions', 1],
            ['Olives', 1],
            ['Zucchini', 1],
            ['Pepperoni', 2]
          ]);
          // Set chart options
          var options = {'title':'How Much Pizza I Ate Last Night',
                         'width':400,
                         'height':300};
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }
        alert("test");
    }
  };
})(jQuery);