<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: link_to_visibility code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 05:52:16 GMT</pubDate>
    <description>DZone Snippets: link_to_visibility code</description>
    <item>
      <title>RJS with toggle_slide if not visible only</title>
      <link>http://snippets.dzone.com/posts/show/3206</link>
      <description>RJS is very powerful. here is a small code snippet that shows how to do a toggle on a div if NOT visible. If the div is visible, it just exchange it. Here is my rhtml code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    &lt;div style="height: 30px"&gt;&lt;br /&gt;        &lt;div style="float: left; padding: 0 0 0 180px"&gt;&lt;br /&gt;            &lt;%= link_to_remote("test_visibility" , &lt;br /&gt;                :with =&gt; "'is_visible=' + Element.visible('list_div')",&lt;br /&gt;                :loading =&gt; "Element.show('getting_results')",&lt;br /&gt;                :complete =&gt; "Element.hide('getting_results')",&lt;br /&gt;                :failure =&gt; "alert('An error occured, please email us directly!')",&lt;br /&gt;                :url =&gt; { :action =&gt; 'one_action' }) %&gt;&lt;br /&gt;        &lt;/div&gt;&lt;br /&gt;        &lt;div id="getting_results" style="float: left; padding: 3px 0 0 30px; display: none;"&gt;&lt;br /&gt;            &lt;%= image_tag "ajax-loader.gif", :class =&gt; "image", :alt =&gt; "loading..." %&gt;&lt;br /&gt;        &lt;/div&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;div id="list_div" style="display: none;" &gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And in my controller, I have the following code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def one_action&lt;br /&gt;      render :update do | page |&lt;br /&gt;        page.replace_html 'list_div', :partial =&gt; 'one_partial'&lt;br /&gt;        if params['is_visible'] == 'false'&lt;br /&gt;          page.visual_effect :toggle_slide, 'list_div', :duration =&gt; 2&lt;br /&gt;        end       &lt;br /&gt;      end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;How it works?&lt;br /&gt;&lt;code&gt;Element.visible('list_div')&lt;/code&gt; is a prototype function that returns false if the element is not visible and true if it is. In the above case, we have the orginial visibility of the div to be "display: none". Thus the first time the action is called, the Ajax call will send the parameter false to the action. This will make the div appear. For subsequent requests, the value of visibility will be true, thus only replacing the div.</description>
      <pubDate>Tue, 26 Dec 2006 15:12:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3206</guid>
      <author>carlharroch (Carl Harroch)</author>
    </item>
  </channel>
</rss>
