简体   繁体   English

带有jQuery ajax成功函数中的参数的google.setOnLoadCallback

[英]google.setOnLoadCallback with parameter inside jquery ajax success function

Sample code: Both of these seem to work ok, to display a message: 示例代码:这两个似乎都可以正常工作,以显示一条消息:

google.load("visualization", "1", { packages: ["corechart"] });
...
$(document).ready(function () {

  google.setOnLoadCallback(function () {
    alert('from inside ready 1');
  });
});

$(document).ready(function () {

  google.setOnLoadCallback(alert('from inside ready 2'));
});

Note: I'm using alert(..) just for debugging purposes - my real code draws charts. 注意:我仅将alert(..)用于调试目的-我的真实代码绘制了图表。 Now, I want to use these techniques inside $.ajax eg : 现在,我想在$ .ajax中使用这些技术,例如:

  $.ajax({
    type: "POST",
    ... 
    success: function (result) {
      if (result.d) {

        $(document).ready(function () {
          alert('sucess');

          // option 1
          google.setOnLoadCallback(function () {
            alert('from inside ready 3');
          });

          // option 2
          // google.setOnLoadCallback(alert('from inside ready 4'));
        });

Now, on ajax success, I can see "sucess" shown, but option 1 doesn't seem to work. 现在,关于ajax成功,我可以看到显示了“成功”,但是选项1似乎不起作用。 ie I don't see "from inside ready 3". 即我看不到“从内部准备好3”。 If I enable the code at option 2, and comment out the code for option 1, I DO see "from inside ready 4". 如果我启用了选项2的代码,并注释掉了选项1的代码,我确实会看到“从内部准备好4”。

So it seems that option 2 works, but not option 1, from a jquery ajax call. 因此,似乎从jquery ajax调用中的选项2起作用了,但没有起作用。 Can anyone shed some light? 谁能阐明一些想法? Is option 2 100% safe to use? 选项2 100%安全使用吗? It seems to work, but all the examples I've seen seem to use option 1. 似乎可行,但是我所看到的所有示例似乎都使用了选项1。

first, you're using the old version of google charts, 首先,您使用的是旧版的Google图表,
the jsapi library should no longer be used, 不再使用jsapi库,
see the release notes ... 请参阅发行说明 ...

The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. 可以通过jsapi加载程序保留的Google图表版本不再保持一致更新。 Please use the new gstatic loader.js from now on. 从现在开始,请使用新的gstatic loader.js

old: <script src="https://www.google.com/jsapi"></script> 旧: <script src="https://www.google.com/jsapi"></script>

current: <script src="https://www.gstatic.com/charts/loader.js"></script> 当前: <script src="https://www.gstatic.com/charts/loader.js"></script>

this will only change the load statement... 这只会改变加载语句...

from... 从...

google.load("visualization", "1", { packages: ["corechart"] });

to... 至...

google.charts.load("current", { packages: ["corechart"] });

next, you don't need to use the callback every time you need to draw a chart, 接下来,您不需要在每次绘制图表时都使用回调函数,
it only needs to be used once, to ensure google charts has been loaded. 它只需要使用一次,以确保Google图表已加载。

and there are several ways to use the callback, 还有使用回调的几种方法,
you can use the updated setOnLoadCallback function. 您可以使用更新的setOnLoadCallback函数。

google.charts.setOnLoadCallback(drawChart);

or you can place the callback directly in the load statement. 或者您可以将回调直接放在load语句中。

google.charts.load('current', {
  callback: drawChart,
  packages: ['corechart']
});

or what I prefer, the promise it returns. 或我更喜欢的是它的回报。 (google includes a promise polyfill for IE) (Google包括针对IE的Promise Polyfill)

google.charts.load('current', {
  packages: ['corechart']
}).then(drawChart);

now to the question at hand, 现在要解决的问题是
google's load statement will wait for the document to load by default, 谷歌的load语句将默认等待文档加载,
so you can use google.charts.load in place of $(document).ready 因此您可以使用google.charts.load代替$(document).ready

recommend loading google first, then using ajax to get data, then draw your charts. 建议先加载Google,然后使用Ajax获取数据,然后绘制图表。

something similar to the following setup... 类似于以下设置...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {

  // get data for chart 1
  $.ajax({
    type: 'POST',
    ...
  }).done(function (result) {

    drawChart1(result.d);

  }).fail(function (jqXHR, status, errorThrown) {
    console.log(errorThrown);
  });

  // get data for chart 2
  $.ajax({
    type: 'POST',
    ...
  }).done(function (result) {

    drawChart2(result.d);

  }).fail(function (jqXHR, status, errorThrown) {
    console.log(errorThrown);
  });

});

function drawChart1(chartData) {
  ...
}

function drawChart2(chartData) {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM