<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag ajax</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/ajax</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Observing Fields With Rails in the Browser</title>
      <description>&lt;div style='width:160; float:left; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; border-style:solid;margin-bottom:30px; margin-right:30px;' class='tease-image'&gt;
&lt;img width='160' height='240' alt='observation car' src='http://farm1.static.flickr.com/205/443042586_8194430e7a_m.jpg'&gt;
&lt;br/&gt;
&lt;a href='http://flickr.com/photos/dustbowlballad/443042586/'&gt;observation car&lt;/a&gt;
&lt;br/&gt;&amp;copy;
&lt;a href='http://flickr.com/people/dustbowlballad'&gt;carolyn&lt;/a&gt;
&lt;br/&gt;&lt;a href='http://creativecommons.org/licenses/by-nc/2.0/'&gt;&lt;img src='http://i.creativecommons.org/l/by-nc/2.0/80x15.png' title='used under a Creative Commons Attribution-NonCommercial License' width='80' height='15' border='0'/&gt;&lt;/a&gt;
&lt;/div&gt;
The AJAX support in Rails is great, but sometimes you don't want to have to make an unnecessary trip to the server to make your UI dynamic. For example, let's say you want to do something like Basecamp does when you create a new message.  If you have a Basecamp setup to allow you to talk to both members of your team and also clients, it provides some nice checkboxes to let you control who will get notified of the new message.  Those checkboxes are grouped with a checkbox for each company, and then checkboxes for all of the people from that company below the company checkbox. If you check or uncheck a company checkbox all of the people listed under the company are checked or unchecked to match.
&lt;p&gt;This can all be done within the browser.  It just requires manipulation of existing elements within the domain object model, so there's no need to go back to the server.&lt;/p&gt;
&lt;p&gt;Rails provides the &lt;strong&gt;observe_field&lt;/strong&gt; helper method which is normally used to generate a remote request when a DOM element changes.  It can be used to do what we want, but how to do so isn't well documented, either in the Rails documentation, or via google.&lt;/p&gt;
&lt;p&gt;So here's how to do it.&lt;p&gt;



&lt;h2&gt;What the Rails Doc says&lt;/h2&gt;
&lt;p&gt;Here's how the official doc for observe_field starts...&lt;/p&gt; 
&lt;blockquote&gt;
&lt;b&gt;observe_field&lt;/b&gt;(field_id, options = {})
&lt;p&gt;
Observes the field with the DOM ID specified by &lt;tt&gt;field_id&lt;/tt&gt; and calls
a callback when its contents have changed. The default callback is an Ajax
call. By default the value of the observed field is sent as a parameter
with the Ajax call.
&lt;/p&gt;
&lt;p&gt;
Required &lt;tt&gt;options&lt;/tt&gt; are either of:
&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td valign="top"&gt;&lt;tt&gt;:url&lt;/tt&gt;:&lt;/td&gt;&lt;td&gt;&lt;tt&gt;url_for&lt;/tt&gt;-style options for the action to call when the field has
changed.

&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td valign="top"&gt;&lt;tt&gt;:function&lt;/tt&gt;:&lt;/td&gt;&lt;td&gt;Instead of making a remote call to a URL, you can specify a function to be
called instead.

&lt;/td&gt;&lt;/tr&gt;

&lt;/table&gt;
&lt;/blockquote&gt;
&lt;p&gt;So the &lt;b&gt;function&lt;/b&gt; option seems to be what we need  When I read this I assumed that the value should be a string containing a javascript function definition. But it's not.&lt;/p&gt;
&lt;p&gt;What the value should be is javascript code for the &lt;b&gt;body&lt;/b&gt; of the function definition.  The prototype helper provides the surrounding definition.  So when you code:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;        
observe_element('company1', :function =&amp;gt; 'code')&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
The javascript code generated looks like:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;Form.Observer.new('company1', function(element,value) {code})&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So just write Javascript code for the body.  You can use the element parameter to refer to the domain element being observed, in this case it would be the same as the prototype expression "$('company1')"  and the value parameter gives you the value attribute of the observed element.&lt;/p&gt;
So let's say our company has two guys whose checkboxes are 'bill_c' and 'sam_r' and our company checkbox is called 'our_corp'.  Then this helper call:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;        
observe_element(
    'our_corp', 
    :function =&amp;gt; &amp;quot;$('bill_c') = value\n$('sam_r') = value&amp;quot;)&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Should do the trick of turning both the bill_c and sam_r checkboxes on or off when we toggle the our_corp checkbox.&lt;/p&gt;
&lt;p&gt;Of course you probably don't want to hand code the ids, and the actual ids will conform to those used by the form helpers, but I'll leave those details to you&lt;/p&gt;
&lt;h2&gt;Getting the Rails Docs Updated&lt;/h2&gt;
&lt;p&gt;I've submitted a &lt;a href="http://dev.rubyonrails.org/ticket/9808"&gt;ticket on rails trac&lt;/a&gt; with a patch to document this.  If you've got a rails trac account, please have a look and consider adding a +1 to get this into report #12 and into Rails.&lt;/p&gt;</description>
      <pubDate>Sun, 07 Oct 2007 12:34:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:f816b609-8767-4817-aa66-523db46cb82b</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/10/07/observing-fields-with-rails-in-the-browser</link>
      <category>rails</category>
      <category>ajax</category>
      <category>prototype</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/468</trackback:ping>
    </item>
  </channel>
</rss>
