<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: RJS code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 05:52:16 GMT</pubDate>
    <description>DZone Snippets: RJS 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>
    <item>
      <title>how to escape from AJAX URL</title>
      <link>http://snippets.dzone.com/posts/show/1705</link>
      <description>From Dylan Stamat's post on the Ruby on Rails list 	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Within my "login.rjs" template that get's invoked after my login process, it returns to the same page with an error message on error, or... if the login was successful, it needs to "redirect" to another controller... which is pretty much impossible to do otherwise.  so, my " login.rjs" looks like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if @logged_in_client&lt;br /&gt;  page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;else&lt;br /&gt;  page.redirect_to "whatever you want here"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;------ End Message--------&lt;br /&gt;Here is my two cents:&lt;br /&gt;Technically, you can do this without a RJS template at all, because it is such a simple example. Use this in your controller&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;render :update do |page|&lt;br /&gt;  if @logged_in_client&lt;br /&gt;    page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;  else&lt;br /&gt;    page.redirect_to "whatever you want here"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 16 Mar 2006 08:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1705</guid>
      <author>heavysixer ()</author>
    </item>
  </channel>
</rss>
