Poetry of Programming

Its about Ruby on Rails – Kiran Soumya

By

Conditional periodically_call_remote in Rails

Loading new sections or pages in a Rails app using AJAX can do a lot to increase overall application snappiness and lengthen the life cycle of each page load, but if any of these pages use the periodicallycallremote helper, then these periodical calls may persist longer than you would like.

The trick to fix this is to note that in the documentation for periodicallycallremote, the options for defining the target URL and callbacks are the same for the link_to_remote helper, which include an option called :condition.

So if you want your periodical calls to cease when different pages in your app are loaded using AJAX, then use the :condition option to have periodicallycallremote check against a variable:

<%= periodicallycallremote(
:condition => “check_var == true”,
:update => “myupdatediv”,
:frequency => 20,
:url => { :action => “bar” }) %>

Then in your templates define check_var true or false depending on whether you want the periodical updates to take place:

< script type=”text/javascript”>

 check_var = true;

The :condition option can take any sort of JavaScript code, you can be as creative as you like in defining when and under exactly what conditions your periodical calls are made. Take back control!

Leave a Reply

Your email address will not be published. Required fields are marked *