<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Tempest of Thoughts &#187; Prototype.js</title>
	<atom:link href="http://tempe.st/category/prototypejs/feed/" rel="self" type="application/rss+xml" />
	<link>http://tempe.st</link>
	<description>aka blog.to_int(:inig)</description>
	<lastBuildDate>Thu, 13 May 2010 09:54:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Rails and JavaScript: page.call gotchas</title>
		<link>http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/</link>
		<comments>http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/#comments</comments>
		<pubDate>Fri, 31 Aug 2007 13:38:17 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Prototype.js]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/</guid>
		<description><![CDATA[Ruby on Rails has wonderful out of the box javascript support, but sometimes implementing dynamic user interfaces is not so easy as it seems. In an application I&#8217;m working on I have a list of people with a checkbox each. In the load event of the page I add a click handler to every checkbox [...]]]></description>
			<content:encoded><![CDATA[	<p>Ruby on Rails has wonderful out of the box javascript support, but sometimes implementing dynamic user interfaces is not so easy as it seems.</p>
	<p>In an application I&#8217;m working on I have a list of people with a checkbox each. In the load event of the page I add a click handler to every checkbox using this javascript code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.ConfermaInvitati'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> clickHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// ... </span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> clickHandler<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> Event.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Updater</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'my/invited/toggle'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> e.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">id</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onLoading<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> e.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/images/admin/spinner.gif&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Request</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'my/refresh'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>

	<p>This works fine until I add a new person via an Ajax call. That person won&#8217;t have a <em>clickHandler</em> because the element wasn&#8217;t on the page when I called the click handler. So I thought it was time to test <em>page.call</em> in the  <em>render :update</em> block I had in the rails application.</p>
	<p>I tried this code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">render <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#008000; font-style:italic;"># Do stuff that creates the new objects and adds it to the page</span>
  <span style="color:#008000; font-style:italic;"># The data I need is in @invited</span>
  page.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Event.observe($$('##{@invited.permalink} .ConfermaInvitati').first()), 'click', clickHandler)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

	<p>Obviously that didn&#8217;t work, and it turned out I have to read documentation before doing fancy things <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>The rails docs told me that I had to use <em>call</em> passing the function name as the first argument and an array of parameters as the second argument, the problem is that <em>call</em> turns all the parameters into strings&#8212;this means I could not pass the <em>clickHandler</em> function to <em>Event.observe</em>. </p>
	<p>I found the solution in <em><<</em>. If you do <em>page << "foo"</em>, foo will be evaluated as raw javascripts. This meant I was able to do</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;Event.observe($$('##{@invited.permalink} .ConfermaInvitati').first()), 'click', clickHandler)</span></pre></div></div>

	<p>and finally have the functionality I was looking for. So remember, don&#8217;t <em>page.call</em> if you need to pass javascript variables to your functions.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/08/rails-and-javascript-pagecall-gotchas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
