<?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>solnic on blog &#187; unobtrusive</title>
	<atom:link href="http://blog.solnic.eu/tag/unobtrusive/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.solnic.eu</link>
	<description>web development, ruby, javascript and other crazy things</description>
	<lastBuildDate>Mon, 19 Oct 2009 06:24:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unobtrusive JavaScript helpers in Rails 3</title>
		<link>http://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3</link>
		<comments>http://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3#comments</comments>
		<pubDate>Mon, 07 Sep 2009 22:10:46 +0000</pubDate>
		<dc:creator>solnic</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[unobtrusive]]></category>

		<guid isPermaLink="false">http://blog.solnic.eu/?p=186</guid>
		<description><![CDATA[
			
				
			
		A while ago I have written a post about JavaScript helpers in Ruby on Rails and tried to explain why they are a bad idea. It&#8217;s hard to believe for me that it was almost 2 years ago! Since then so many things have happened in the Ruby world&#8230;Now Rails 3 is on its way [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a  href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.solnic.eu%2F2009%2F09%2F08%2Funobtrusive-javascript-helpers-in-rails-3">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.solnic.eu%2F2009%2F09%2F08%2Funobtrusive-javascript-helpers-in-rails-3&amp;source=s0lnic&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>A while ago I have written a post about <a  href="http://blog.solnic.eu/2007/10/30/why-javascript-helpers-in-rails-are-evil">JavaScript helpers in Ruby on Rails</a> and tried to explain why they are a bad idea. It&#8217;s hard to believe for me that it was almost 2 years ago! Since then so many things have happened in the Ruby world&#8230;Now Rails 3 is on its way and we already know what significant improvements and changes it will include. One of them is related to JavaScript helpers and the way how remote links and forms will be handled and I must admit that the new idea is absolutely great.</p>

<p>The new way is based on unobtrusive approach to JavaScript. This means that <span class="caps">HTML </span>code will be separated from JavaScript. I have checked out the latest sources of Ruby on Rails and found out that some of the work is already done. There is a new helper called AjaxHelper, it implements link_to_remote method which in the moment of writing this post looks like this:</p>


<div class="wp_codebox"><table><tr id="p1864"><td class="code" id="p186code4"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> link_to_remote<span style="color:#006600; font-weight:bold;">&#40;</span>name, url, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  html = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:html</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  update = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:update</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> update.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    html<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data-update-success&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = update<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:success</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    html<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data-update-failure&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = update<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:failure</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    html<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data-update-success&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = update
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  html<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data-update-position&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:position</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  html<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data-method&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>          = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:method</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  html<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;data-remote&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>          = <span style="color:#996600;">&quot;true&quot;</span>
&nbsp;
  html.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  url = url_for<span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> url.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  link_to<span style="color:#006600; font-weight:bold;">&#40;</span>name, url, html<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<p>What you see here will generate a clean markup with <span class="caps">HTML5</span>-compliant attributes prefixed with a word &#8220;data-&#8221;. If you are not familiar with them you can checkout a nice article by John Resig <a  href="http://ejohn.org/blog/html-5-data-attributes"><span class="caps">HTML</span> 5 data- Attributes</a>. Those attributes will instruct the additional JavaScript code how it should handle the behavior. Basically all links, buttons and forms that have the special attribute &#8220;data-remote&#8221; set to &#8220;true&#8221; will issue an <span class="caps">AJAX </span>request. There has been a discussion on <a  href="http://www.mail-archive.com/rubyonrails-core@googlegroups.com/msg09122.html">the Rails on Rails Core group</a> about how to implement corresponding JavaScript code. People are worried about its performance since finding all elements with data-remote=true appears to be slow in case of Prototype and jQuery. Moreover there is a problem of new elements that may be dynamically inserted after the page was loaded and all the event listeners were attached. Fortunately there is no need to be worried as our situation is a perfect example where we should use <a  href="http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/">Event Delegation</a>. <span class="caps">DHH </span>has already showed in his <a  href="http://www.scribd.com/doc/15010095/Rails-3-and-the-Real-Secret-to-High-Productivity">Rails 3 and the Real Secret to High Productivity</a> presentation how links and buttons can be handled by <a  href="http://www.prototypejs.org">Prototype</a> library and it looks absolutely reasonable to me.</p>

<p>I would like to focus on jQuery though as it&#8217;s getting more popular even in the Rails community. Great example is <a  href="http://www.lunarlogicpolska.com">my job</a> where we use jQuery in every of our new projects. So how can we handle new remote links and forms using this popular library? Actually it&#8217;s ridiculously easy. Thanks to <a  href="http://docs.jquery.com/Events/live">jQuery.live</a> function we can easily use <a  href="http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think">Event Delegation</a> to handle <span class="caps">AJAX </span>calls. Just take a look at this sample of a markup that new helpers in Rails 3 will generate:</p>


<div class="wp_codebox"><table><tr id="p1865"><td class="code" id="p186code5"><pre class="html" style="font-family:monospace;">&lt;!-- the new link to remote --&gt;
&lt;a href=&quot;/users&quot; data-remote=&quot;true&quot;&gt;Users&lt;/a&gt;
&nbsp;
&lt;!-- the new remote form --&gt;
&lt;form action=&quot;/users&quot; method=&quot;post&quot; data-remote=&quot;true&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;login&quot;/&gt;
  &lt;input type=&quot;submit&quot;/&gt;
&lt;/form&gt;</pre></td></tr></table></div>




<p>Pretty clean, I really like it! Now let&#8217;s see how we can implement jQuery handler that will send <span class="caps">AJAX </span>requests:</p>


<div class="wp_codebox"><table><tr id="p1866"><td class="code" id="p186code6"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> url <span style="color: #339933;">:</span> options.<span style="color: #660066;">url</span><span style="color: #339933;">,</span> type <span style="color: #339933;">:</span> <span style="color: #3366CC;">'get'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</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: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// remote links handler</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[data-remote=true]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><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: #000066; font-weight: bold;">return</span> request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> url <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</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><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// remote forms handler</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form[data-remote=true]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submit'</span><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: #000066; font-weight: bold;">return</span> request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> url <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">action</span><span style="color: #339933;">,</span> type <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">method</span><span style="color: #339933;">,</span> data <span style="color: #339933;">:</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;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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><span style="color: #339933;">;</span></pre></td></tr></table></div>




<p>The above code will send an <span class="caps">AJAX </span>request when you click on a remote link or submit a remote form. Note that it will work also with new elements dynamically inserted to the <span class="caps">DOM.</span> The example JavaScript code is the bare minimum of course, we could have something much more sophisticated. We will be able to specify success and failure handlers and also elements that should be updated with an <span class="caps">AJAX </span>response text (and probably much more!), hence the JavaScript is going to be more complicated.</p>

<p>This is definitely a step into the right direction. I&#8217;m looking forward to Rails 3!</p>]]></content:encoded>
			<wfw:commentRss>http://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
