<?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; Ajax</title>
	<atom:link href="http://tempe.st/category/ajax/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>
		<item>
		<title>A praise for jQuery</title>
		<link>http://tempe.st/2007/05/a-praise-for-jquery/</link>
		<comments>http://tempe.st/2007/05/a-praise-for-jquery/#comments</comments>
		<pubDate>Thu, 17 May 2007 07:55:17 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/05/a-praise-for-jquery/</guid>
		<description><![CDATA[I&#8217;ve been doing some drupal development lately, and had to make the switch from Prototype to jQuery (by the way it&#8217;s the same switch WordPress made in 2.2). At first I had to learn the different approach jQuery has to manipulating the DOM, but once I got the hang of it I really started appreciating [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve been doing some drupal development lately, and had to make the switch from <a href="http://prototypejs.org">Prototype</a> to <a href="http://jquery.com">jQuery</a> (by the way it&#8217;s the same switch WordPress made in 2.2).</p>
	<p>At first I had to learn the different approach jQuery has to manipulating the DOM, but once I got the hang of it I really started appreciating jQuery.</p>
	<p>The main difference I&#8217;ve found is that I prefer programming Prototype via the Rails helpers, while programming jQuery is so fun I prefer to do it directly in JavaScript.</p>
	<p>Actually it turned out so fun that I keep adding content to pages and changes links in JS, while I should do that in the backend.</p>
	<p>Here&#8217;s an example, from a drupal module I wrote:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #3366CC;">'#comments h2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;slider&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;slide-open&quot;&gt;Espandi tutti i commenti&lt;/a&gt; - &lt;a href=&quot;#&quot; class=&quot;slide-close&quot;&gt;Contrai tutti i commenti&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[+]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment h3'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideToggle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;[-]&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[+]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[-]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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: #009900;">&#40;</span><span style="color: #3366CC;">'#comments .slider .slide-open'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[-]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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: #009900;">&#40;</span><span style="color: #3366CC;">'#comments .slider .slide-close'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #3366CC;">'.content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.comment .expand'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;[+]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

	<p>What does this code do? Add a couple of links after the comments heading, hide all the comments and add a slidedown/up functionality to them.</p>
	<p>The best thing of adding this functionality in the frontend is that when the user has js disabled or is using a non-js browser the site falls back to the normal behavior.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/05/a-praise-for-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript Classes</title>
		<link>http://tempe.st/2007/05/javascript-classes/</link>
		<comments>http://tempe.st/2007/05/javascript-classes/#comments</comments>
		<pubDate>Wed, 16 May 2007 09:37:15 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Yahoo]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/05/javascript-classes/</guid>
		<description><![CDATA[Before you go &#8220;hey, but js doesn&#8217;t have classes&#8221; let me tell I meant classes as in &#8220;school classes&#8221; I don&#8217;t usually like repeating myself and writing posts about links I put on my del.icio.us, but this time I have to. I&#8217;m having a great time learning from these videos of Doug Crockford&#8217;s presentation of [...]]]></description>
			<content:encoded><![CDATA[	<p>Before you go &#8220;hey, but js doesn&#8217;t have classes&#8221; let me tell I meant classes as in &#8220;school classes&#8221; <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
	<p>I don&#8217;t usually like repeating myself and writing posts about links I put on <a href="http://del.icio.us/intinig">my del.icio.us</a>, but this time I have to. I&#8217;m having a great time learning from <a href="http://101out.com/js.php">these videos</a> of Doug Crockford&#8217;s presentation of JS @ Yahoo!</p>
	<p>Wonderful stuff there, after 15 minutes of the first video I already learnt some JS tricks I didn&#8217;t know (or didn&#8217;t care about).</p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/05/javascript-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drag and Drop deletion with Rails</title>
		<link>http://tempe.st/2007/04/drag-and-drop-deletion-with-rails/</link>
		<comments>http://tempe.st/2007/04/drag-and-drop-deletion-with-rails/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 18:06:33 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/04/drag-and-drop-deletion-with-rails/</guid>
		<description><![CDATA[I don&#8217;t like to do Drag and Drop in Web Applications. I dislike it so much that I never did dnd in Rails, so when a customer explicitly asked for a drag and drop trashcan in an application I had to start looking through rails docs, fearing dozens of Javascript callbacks. It turned out wonderfully [...]]]></description>
			<content:encoded><![CDATA[	<p>I don&#8217;t like to do Drag and Drop in Web Applications. I dislike it so much that I never did dnd in Rails, so when a customer explicitly asked for a drag and drop trashcan in an application I had to start looking through rails docs, fearing dozens of Javascript callbacks.</p>
	<p>It turned out wonderfully easy, requiring me to add just two lines of code in the view and a short action in the controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"># This is the line of code I added to the partial that is rendered for each draggable element
<span style="color:#006600; font-weight:bold;">&lt;%</span>= draggable_element <span style="color:#996600;">&quot;#{picture.class}_#{picture.id}&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:revert</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:constraint</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;'vertical'&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
# This is the code I added to the trashcan 
<span style="color:#006600; font-weight:bold;">&lt;%</span>= drop_receiving_element<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;trash&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:controller <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:immobili</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:destroy_image</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:hoverclass</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;hover_trash&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

	<p>That&#8217;s it, really, the controller action just deletes the element and hides is on the page via a render :update block.</p>


 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/04/drag-and-drop-deletion-with-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Driven RJS with ARTS</title>
		<link>http://tempe.st/2007/04/test-driven-rjs-with-arts/</link>
		<comments>http://tempe.st/2007/04/test-driven-rjs-with-arts/#comments</comments>
		<pubDate>Wed, 04 Apr 2007 10:40:52 +0000</pubDate>
		<dc:creator>Giovanni Intini</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://tempe.st/2007/04/test-driven-rjs-with-arts/</guid>
		<description><![CDATA[If you&#8217;re like me, you like writing tests for your code. Writing tests is a nice thing to do and saves you a lot of headaches. Writing tests for Ruby on Rails is really simple, with the wonderful testing framework built in Rails, but things become less ideal when you need to write tests for [...]]]></description>
			<content:encoded><![CDATA[	<p>If you&#8217;re like me, you like writing tests for your code. Writing tests is a nice thing to do and saves you a lot of headaches. </p>
	<p>Writing tests for Ruby on Rails is really simple, with the wonderful testing framework built in Rails, but things become less ideal when you need to write tests for your RJS code. </p>
	<p>This morning I wanted to test the following snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> add_file_field
  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>
    page.<span style="color:#9900CC;">remove</span> <span style="color:#996600;">&quot;link_to_add_#{params[:attachment_type]}&quot;</span>
    page.<span style="color:#9900CC;">insert_html</span> <span style="color:#ff3333; font-weight:bold;">:bottom</span>,
                     params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:attachment_type</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
                     <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{params[:attachment_type]}_file_field&quot;</span>, 
                     <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:index <span style="color:#006600; font-weight:bold;">=&gt;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

	<p>But the test was failing and I didn&#8217;t know why. The <a href="http://glu.ttono.us/articles/2006/05/29/guide-test-driven-rjs-with-arts">ARTS Plugin</a> came to the rescue, allowing me to test the rjs response:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> test_add_file_field
    xhr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:get</span>, <span style="color:#ff3333; font-weight:bold;">:add_file_field</span>, <span style="color:#ff3333; font-weight:bold;">:index</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:attachment_type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;plan&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_rjs <span style="color:#ff3333; font-weight:bold;">:remove</span>, <span style="color:#996600;">&quot;link_to_add_plan&quot;</span>
    <span style="color:#008000; font-style:italic;"># more code here...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

	<p>Unfortunately that didn&#8217;t help with my problem since the cause was not the RJS, I was simply forgetting to login in my setup method <img src='http://tempe.st/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

 ]]></content:encoded>
			<wfw:commentRss>http://tempe.st/2007/04/test-driven-rjs-with-arts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
