<?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; ajax</title>
	<atom:link href="http://blog.solnic.eu/tag/ajax/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>
		<item>
		<title>Client-side rendering with Prototype</title>
		<link>http://blog.solnic.eu/2009/01/15/client-side-rendering-with-prototype</link>
		<comments>http://blog.solnic.eu/2009/01/15/client-side-rendering-with-prototype#comments</comments>
		<pubDate>Thu, 15 Jan 2009 21:49:49 +0000</pubDate>
		<dc:creator>solnic</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://blog.solnic.eu/?p=71</guid>
		<description><![CDATA[
			
				
			
		Web applications are getting more and more complex. The user interface of a modern web application can be as rich as its desktop equivalent. If we use JavaScript/HTML/CSS trio to build this UI then we definitely want to use AJAX. A typical approach is to use AJAX to update parts of our page using an [...]]]></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%2F01%2F15%2Fclient-side-rendering-with-prototype">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.solnic.eu%2F2009%2F01%2F15%2Fclient-side-rendering-with-prototype&amp;source=s0lnic&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>Web applications are getting more and more complex. The user interface of a modern web application can be as rich as its desktop equivalent. If we use JavaScript/HTML/CSS trio to build this UI then we definitely want to use <span class="caps">AJAX.</span> A typical approach is to use <span class="caps">AJAX </span>to update parts of our page using an <span class="caps">HTML </span>response, everyone knows that, right? Does this approach allow us to create a responsive, fast and flexible UI? The answer is no.</p>

<p>Here are 4 main downsides of using <span class="caps">AJAX </span>requests to load UI parts:</p>


<ul>
<li>You increase the server load &#8211; yes, you make an <span class="caps">AJAX </span>request only because you need a piece of <span class="caps">HTML </span>code that you want to insert to an already rendered page. That was cool a few years ago when <span class="caps">AJAX </span>was such a great innovation. Nowadays it should be considered as a less efficient solution.</li>
</ul>




<ul>
<li>You complicate the server-side code &#8211; because you need parts of your page to be returned by the server you, obviously, need to handle that by writing more server-side code. You end up having many actions in your controllers that return different fragments of <span class="caps">HTML.</span> I know, you think it&#8217;s normal, everyone does that.</li>
</ul>




<ul>
<li>You loose a lot of control over the UI &#8211; you use <span class="caps">DHTML </span>techniques to deal with the UI and in the same time you need the server to get parts of that <span class="caps">UI.</span> This leads to a code duplication and in many cases ends up with a big mess.</li>
</ul>




<ul>
<li>A user will have to wait until a request is done &#8211; that just sucks, that poor guy has to wait because you went back to the server for a little piece of <span class="caps">HTML.</span> How you are going to implement a responsive UI this way? &#8220;Your servers are fast&#8221;. Of course they are, but what if user&#8217;s <span class="caps">ISP </span>sucks? What if the user is downloading something and 99% of his bandwidth is gone?</li>
</ul>



<p>Rendering <span class="caps">HTML </span>on the client-side is ridiculously simple. Consider the following example. Let&#8217;s say we have a page with a list of some people, the list is just a simple <span class="caps">HTML </span>table. It could look like this:</p>


<div class="wp_codebox"><table><tr id="p7110"><td class="code" id="p71code10"><pre class="html" style="font-family:monospace;">&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;First Name&lt;/th&gt;
      &lt;th&gt;Last Name&lt;/th&gt;
      &lt;th&gt;E-Mail&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody id=&quot;people&quot;&gt;
    &lt;!-- here go people --&gt;
  &lt;tbody&gt;
  &lt;tfoot&gt;
    &lt;tr colspan=&quot;4&quot;&gt;
      &lt;td&gt;
        &lt;a id=&quot;previous&quot; href=&quot;#&quot;&gt;Previous&lt;/a&gt; 
        &lt;a id=&quot;next&quot; href=&quot;#&quot;&gt;Next&lt;/a&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tfoot&gt;
&lt;/table&gt;</pre></td></tr></table></div>




<p>As you can see the tbody element is empty, we will load its content after the entire page is loaded. Instead of a bunch of &#8220;tr&#8221; elements the server should return a nice <span class="caps">JSON </span>data which could look like this:</p>


<div class="wp_codebox"><table><tr id="p7111"><td class="code" id="p71code11"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
 <span style="color: #009900;">&#123;</span> id <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> first_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'John'</span><span style="color: #339933;">,</span> last_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Doe'</span><span style="color: #339933;">,</span> email <span style="color: #339933;">:</span> <span style="color: #3366CC;">'john.doe@somewhere.com'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
 <span style="color: #009900;">&#123;</span> id <span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> first_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Jane'</span><span style="color: #339933;">,</span> last_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Doe'</span><span style="color: #339933;">,</span> email <span style="color: #339933;">:</span> <span style="color: #3366CC;">'jane.doe@anywhere.com'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
 <span style="color: #009900;">&#123;</span> id <span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> first_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Third'</span><span style="color: #339933;">,</span> last_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Guy'</span><span style="color: #339933;">,</span> email <span style="color: #339933;">:</span> <span style="color: #3366CC;">'third.guy@nowhere.com'</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>




<p>To render these data we obviously need a template. Prototype gives us a handy class called <a  href="http://prototypejs.org/api/template">Template</a> which is perfect for our needs. To create a template and render the list of people we need a few lines of JavaScript:</p>


<div class="wp_codebox"><table><tr id="p7112"><td class="code" id="p71code12"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> personRowTemplate <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #009900;">&#40;</span>
  <span style="color: #3366CC;">'&lt;tr id=&quot;person_#{id}&quot;&gt;&lt;td&gt;#{first_name}&lt;/td&gt;&lt;td&gt;#{last_name}&lt;/td&gt;&lt;td&gt;#{email}&lt;/td&gt;&lt;/tr&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> peopleRows <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
people.<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>person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  peopleRows <span style="color: #339933;">+=</span> personRowTemplate.<span style="color: #660066;">evaluate</span><span style="color: #009900;">&#40;</span>person<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: #009900;">&#40;</span><span style="color: #3366CC;">'people'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>peopleRows<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>




<p>That&#8217;s pretty much it. It doesn&#8217;t look spectacular, huh? Now just think about the benefits of this approach:</p>


<ul>
<li>You have the data in <span class="caps">JSON </span>format, you can use them to render things like an edit form without going back to the server, thus user experience will be better</li>
</ul>




<ul>
<li>You can be focused on building a nice <span class="caps">JSON API </span>for accessing data instead of implementing actions that return small pieces of <span class="caps">HTML</span></li>
</ul>




<ul>
<li>Your client-side code is cleaner and more consistent</li>
</ul>



<p>Sounds like a good deal, doesn&#8217;t it? <img src='http://blog.solnic.eu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.solnic.eu/2009/01/15/client-side-rendering-with-prototype/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
