<?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>Coffee Powered &#187; erb</title>
	<atom:link href="http://www.coffeepowered.net/tag/erb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coffeepowered.net</link>
	<description>code and content</description>
	<lastBuildDate>Mon, 09 Jan 2012 18:32:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Hold the RJS, please.</title>
		<link>http://www.coffeepowered.net/2008/09/26/hold-the-rjs-please/</link>
		<comments>http://www.coffeepowered.net/2008/09/26/hold-the-rjs-please/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 00:00:35 +0000</pubDate>
		<dc:creator>Chris Heald</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[erb]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rjs]]></category>

		<guid isPermaLink="false">http://www.coffeepowered.net/?p=30</guid>
		<description><![CDATA[Rails does a great job at many things, but one of the most consistent stumbling blocks I see in #rubyonrails is RJS. It complicates many things that just don&#8217;t need to be that complex, especially when using a Javascript helper library like Prototype or jQuery. Keep things simple! One little helper, and your AJAX requests [...]]]></description>
			<content:encoded><![CDATA[<p>Rails does a great job at many things, but one of the most consistent stumbling blocks I see in <a href="http://wiki.rubyonrails.org/rails/pages/IRC">#rubyonrails</a> is RJS. It complicates many things that just don&#8217;t need to be that complex, especially when using a Javascript helper library like <a href="http://prototypejs.org/">Prototype</a> or <a href="http://jquery.com/">jQuery</a>.</p>
<p>Keep things simple! One little helper, and your AJAX requests get a whole lot easier to manage. This was inspired by the now-defunct <a href="http://www.danwebb.net/2006/11/24/minusmor-released">MinusMOR</a>.</p>
<pre class="brush: ruby; title: ; notranslate">
def js(data)
  data.respond_to?(:to_json) &amp;&amp; data.to_json || data.inspect.to_json
end
</pre>
<p>Now, when you want to render a Javascript view, you can use just straight Javascript. For example, if you would like to update a given element in your page with the contents of a partial, save a template, something like <code>update.js.erb</code>, with the following:</p>
<pre class="brush: jscript; title: ; notranslate">$(&quot;div#post_&lt;%=@post.id%&gt;&quot;).update(&lt;%=js render(:partial =&gt; &quot;post.html.erb&quot;, :object =&gt; @post) %&gt;);</pre>
<p>This is admittedly more complex than RJS in the simple case, but what about when you want to do more complex stuff, like this <code>vote.js.erb</code> template?</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;% unless @msg.nil? %&gt;
statusMessage(&lt;%=js @msg%&gt;);
&lt;% end %&gt;
var obj = $$('div#post_&lt;%=@post.id%&gt; span.vote a');
for(var i=0; i&lt;obj.length; i++) {
	var e = obj[i];
	e.update(&lt;%=js &quot;+#{@post.vote_score}&quot;%&gt;);
	e.addClassName(&quot;voted_post&quot;);
}
</pre>
<p>Just as easy as writing any of your other views, and won&#8217;t get in your way when you need to go some fancy Javascript gymnastics without requiring ugly heredocs cluttering up the code all over.</p>
<p>How about passing an <code>Array</code> to a client-side Javascript function?</p>
<pre class="brush: jscript; title: ; notranslate">
runSomeUpdateFunctionThatTakesAJavascriptArray(&lt;%=js @list_of_values %&gt;)
</pre>
<p>No worries about malformed Javascript!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coffeepowered.net/2008/09/26/hold-the-rjs-please/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

