0

Below file is in .html.erb i want to call mash again and again how i do it.I tried many times through setInterval but it getting old value of data.I am getting new value in mesh data.but when i see second time calling it takes old.

    <%

        mash = {
          "id" => id, "legends" => legends, "x_units" => x_units, "y_units" => y_units, "data" => data, "alertPoints" => alert_points, "time_mode" => time_mode, 
          "colors" => colors, "x_label" => x_label, "y_label" => y_label, "tipColors" => tipColors, "dateformat" => @dateformat, "timeformat" => @timeformat, 
          "chart_url" => chart_url, "x_label_date_format" => x_label_date_format, "turn_series" => turn_series, "y_ticks_array" => y_ticks_array, "y_label_width" => y_label_width,
          "series_type" => series_type,"vertical_threshold" => vertical_threshold, "x_label_suffix" => x_label_suffix, "y_tick_on" => y_tick_on, "xmin" => xmin, "xmax" => xmax
          }
        %>


            <script type="javascript">

                  setInterval(    function ()
                 {
                 onDataReceived(<%=mash.to_json.html_safe%>);



                 }, 5000);
              </script>
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • how are you fetching your data? What's your ajax callback like? Kinda hard to tell whats wrong without anymore details... – Abraham P Feb 02 '13 at 06:26
  • if use put command to see mash[data] it dynamically change.but <%=mash.to_json.html_safe%> it getting old value .can i call above mash again and again – aiyaz ahmad Feb 02 '13 at 06:40

1 Answers1

1

The issue is that mash is evaluated once when the page is rendered, so using the erb again and again will always produce the same result. If you want new data, you probably need to request it with AJAX.

In Rails, how do you render JSON using a view?

Docs to make ajax calls with jQuery: http://api.jquery.com/jQuery.ajax/

Ex:

setInterval(function() { 
  $.get('route/to/mash.json', function(data) {
     onDataReceived(data);
  });
}, 5000);
Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118