<?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>Sentia &#124; Sydney IT Consultancy, Software Development, Ruby on Rails, Web Application Development, Rails Development, Test Driven Development, Microsoft.Net, Asp.Net , Agile, Continuous Integration Training, iPhone development &#187; rjs</title>
	<atom:link href="http://www.sentia.com.au/tag/rjs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sentia.com.au</link>
	<description>Sentia company website and blog about all things development, Ruby on Rails, Microsoft .Net, ASP.Net, C#.Net, Agile web development, Test Driven Development</description>
	<lastBuildDate>Thu, 02 Feb 2012 07:16:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using Javascript Code for RJS Instead of IDs</title>
		<link>http://www.sentia.com.au/2009/03/using-javascript-code-for-rjs-instead-of-ids/</link>
		<comments>http://www.sentia.com.au/2009/03/using-javascript-code-for-rjs-instead-of-ids/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 11:47:24 +0000</pubDate>
		<dc:creator>Michael Cindric</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rjs]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://sentia.com.au/?p=175</guid>
		<description><![CDATA[UPDATE: JavascriptGenerator has a method name literal that will do &#8230; <a href="http://www.sentia.com.au/2009/03/using-javascript-code-for-rjs-instead-of-ids/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p> UPDATE: JavascriptGenerator has a method name literal that will do the exact same thing, so the code below should do the trick:</p>
<pre class="brush: ruby; title: ; notranslate">
 page.insert_html :bottom, page.literal(&quot;$$('p.welcome b').first()&quot;), &quot;Some item&quot;
</pre>
<p>Bill Burhcam over at <a href="http://www.meme-rocket.com/2007/04/10/fight-id-proliferation-and-update-any-element-you-want/">Meme Rocket</a> made a post that *almost* solved a problem for me the other day. We&#8217;re currently doing some work that ends up creating html. Pretty standard stuff, but for a few reasons that aren&#8217;t really worth going into we can&#8217;t easily rely on our element ids being unique. The prototype functions &#8216;up&#8217; and &#8216;down&#8217; are perfect for this situation though.</p>
<p>So our problem basically boils down to this &#8211; RJS converts this code:</p>
<pre class="brush: ruby; title: ; notranslate">
page.insert_html :bottom, &quot;$$('p.welcome b').first()&quot;, &quot;Some item&quot;
</pre>
<p>into this javascript:</p>
<pre class="brush: jscript; title: ; notranslate">
 new Insertion.Bottom(&quot;$$('p.welcome b').first()&quot;, &quot;Some item&quot;
</pre>
<p>Because the $$(&#8216;p&#8230;&#8217;) is in quotes, Insertion.Bottom just looks for an element with that id. Which isn&#8217;t what we really wanted.</p>
<p>After digging around in the ActionView source for a while I found JavascriptGenerator::GeneratorMethods and its javascript_object_for method. It turns out that if you pass in a ActiveSupport::JSON::Variable then insert_html will behave how we want. So for the example above, the code below will work as expected.</p>
<pre class="brush: ruby; title: ; notranslate">
 js_literal = &quot;$$('p.welcome b').first()&quot; js_literal = ActiveSupport::JSON::Variable.new(js_literal) page.insert_html :bottom, js_literal, &quot;Some item&quot;
</pre>
<p>will output:</p>
<pre class="brush: ruby; title: ; notranslate">
 new Insertion.Bottom($$('p.welcome b').first(), &quot;Some item&quot;
</pre>
<p>Thank you open source. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sentia.com.au/2009/03/using-javascript-code-for-rjs-instead-of-ids/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

