Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.63 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to re-render a graph at a given interval that retrieves data from a controller in Rails?
  2. var ajax_path = #{action_controller_path}
  3. function pollData() {
  4.   $.ajax({
  5.     type: 'get',
  6.     url: ajax_path,
  7.     success: function(data) {
  8.       // update chart
  9.       var series = chart.series[0],
  10.             shift = series.data.length > 20; // shift if the series is longer than 20          
  11.       chart.series[0].addPoint(data, true, shift);
  12.  
  13.       setTimeout(pollData, 60000) // Poll data every 60s
  14.     }
  15.   });
  16. }
  17.        
  18. respond_to :json, only: [:polling_action]
  19. def polling_action
  20.   @data_point = Model.get_data
  21.   respond_with @data_point
  22. end