<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Blah Blah Woof Woof comments on Conditional periodically_call_remote in Rails</title>
    <link>http://vault.openmonkey.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Blah Blah Woof Woof comments</description>
    <item>
      <title>"Conditional periodically_call_remote in Rails" by triley</title>
      <description>&lt;p&gt;Loading new sections or pages in a Rails app using &lt;span class="caps"&gt;AJAX&lt;/span&gt; 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 &lt;code&gt;periodically_call_remote&lt;/code&gt; helper, then these periodical calls may persist longer than you would like.&lt;/p&gt;


	&lt;p&gt;The trick to fix this is to note that in the &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000434"&gt;documentation for periodically_call_remote&lt;/a&gt;,  the options for defining the target &lt;span class="caps"&gt;URL&lt;/span&gt; and callbacks are the same for the &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000433"&gt;link_to_remote helper&lt;/a&gt;, which include an option called &lt;code&gt;:condition&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;So if you want your periodical calls to cease when different pages in your app are loaded using &lt;span class="caps"&gt;AJAX&lt;/span&gt;, then use the &lt;code&gt;:condition&lt;/code&gt; option to have &lt;code&gt;periodically_call_remote&lt;/code&gt; check against a variable:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
 &amp;lt;%= periodically_call_remote(
   :condition =&amp;gt; "check_var == true",
   :update =&amp;gt; "my_update_div",
   :frequency =&amp;gt; 20,
   :url =&amp;gt; { :action =&amp;gt; "bar" }) %&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Then in your templates define &lt;code&gt;check_var&lt;/code&gt; true or false depending on whether you want the periodical updates to take place:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
 &amp;lt;script type="text/javascript"&amp;gt;
   //&amp;lt;![CDATA[
     check_var = true;
   //]]&amp;gt;
 &amp;lt;/script&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;:condition&lt;/code&gt;  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!&lt;/p&gt;

</description>
      <pubDate>Mon, 23 Jan 2006 18:53:00 PST</pubDate>
      <guid>&lt;a href="/articles/2006/01/23/conditional-periodically_call_remote-in-rails"&gt;Conditional periodically_call_remote in Rails&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2006/01/23/conditional-periodically_call_remote-in-rails"&gt;Conditional periodically_call_remote in Rails&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
