2

Using the Query object in the Google Visualization API, I need to find a way to abort a request. I set up the query normally:

var qMyQuery=new google.visualization.Query('http://myurl');
qMyQuery.send(queryDoneCallbackFunctionName);

I have tried the .abort method, but that only applies to regularly refreshing data. I have also tried setting qMyQuery=null, but that didn't do anything.

I cannot simply discard the data when it is returned to the callback... the point of this is to stop a very lengthy query on the server, if requested by the user.

Any suggestions? Thanks.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • If your goal is to stop a lengthy query, why not use the setTimeout method to limit how long you're willing to wait? – oli Jul 04 '11 at 21:52
  • @oli, I want to cancel the query if the user clicks a cancel button in my application, not automatically based on a paritcular time. – Brad Jul 04 '11 at 21:56
  • Have you tried qMyQuery.abort()? Saw this here: http://code.google.com/apis/chart/interactive/docs/examples.html#querywrapper. – Jonathan Pitre Jul 06 '11 at 17:08
  • @Nathan, Yes, as it says in my post. The abort method only aborts an interval refresh, and not a one-off query. – Brad Jul 06 '11 at 17:26

2 Answers2

1

The simplest solution would seem to be to send the request to google manually using a custom XHR object, jQuery, ExtJS, etc. Then you would have full power to cancel the request on command from a user through button click handlers or whatever you would like.

Google's visualization query module does allow it to display errors automatically to the user in the chart DIV, but otherwise, can't you just use your own XHR object?

Either build a custom XHR object, use jQuery, ExtJS Ajax, or something of the sort.

WattsInABox
  • 4,548
  • 2
  • 33
  • 43
  • Yes, that is the route I will probably end up going. There is no way to find the XHR object within the Google Query object and add a second abort method to it? I can't do something with its prototype? – Brad Jul 10 '11 at 16:12
  • You could, but then what happens when google updates their API? Potential breaking changes. If you can view the source, then this will be easy. If not, then I wouldn't recommend it. – WattsInABox Jul 11 '11 at 18:39
  • an example would be helpful :) – Christopher Manning Jul 13 '11 at 16:37
  • @Christopher Manning, you would like an example of overwriting the XHR object's prototype? – WattsInABox Jul 14 '11 at 20:16
  • @dmf85 that would be helpful since I couldn't see how to override the request generated by the google visualization query module – Christopher Manning Jul 14 '11 at 20:28
0

If you simply want to cancel the Ajax request in the client side you should look this question Abort Ajax requests using jQuery

But to save the Server resources you have to emit a new AJAX request, consuming another set of resources both in the client and in the server.

The server side implementation of such abort request would require the verification of a flag which would be stored in a shared location, such as a database.

You have to calculate or measure carefully to see the cost-benefit of such implementation

Community
  • 1
  • 1
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • 1
    I can't use this method, as I can't get the underlying xhr object from a Google Query object. See http://code.google.com/apis/chart/interactive/docs/reference.html#Query – Brad Jul 08 '11 at 14:08
  • 1
    @Brad if you can't get the the xhr object, and the API doesn't provide any way to cancel you can't cancel the request. Unless you rewrite the API, of course. – Jader Dias Jul 08 '11 at 16:51