<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<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/"
	>

<channel>
	<title>Phil Rathe.com</title>
	<link>http://philrathe.com</link>
	<description>Prog, Tech, Tux and Dev Stuff</description>
	<pubDate>Mon, 04 Feb 2008 14:10:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>JavaScript first-class function limitation</title>
		<link>http://philrathe.com/2008/02/03/javascript-scope-and-sequence-execution-can-surprise-you/</link>
		<comments>http://philrathe.com/2008/02/03/javascript-scope-and-sequence-execution-can-surprise-you/#comments</comments>
		<pubDate>Sun, 03 Feb 2008 20:14:22 +0000</pubDate>
		<dc:creator>Philippe Rathe</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://philrathe.com/2008/02/03/javascript-scope-and-sequence-execution-can-surprise-you/</guid>
		<description><![CDATA[Yesterday I needed to use a specific Object Oriented pattern to program a JavaScript module. My module was kind of a big object that contains other objects (sub-module).
The object literal notation
var bigModule = &#123;
&#160; &#160; &#160; &#160; subModule1: &#123;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; method1: function &#40;&#41; &#123; &#125;,
&#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I needed to use a specific Object Oriented pattern to program a JavaScript module. My module was kind of a big object that contains other objects (sub-module).</p>
<h3>The object literal notation</h3>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> bigModule = <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; subModule1: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; subModule2: <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span>;</div>
<p>Because I needed to use closures for the module and it&#8217;s sub-modules, so I can keep the state of my system within the proper module, I didn&#8217;t want to use the object literal notation to code my module.</p>
<p>Instead I used functions to return my module properties, so I can declare variables just above the return statement of the module, so every properties can access them EVEN WHEN the function will return, e.g. until the program is loaded in memory.</p>
<h3>The real way I did it</h3>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> bigModule = <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> foo;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subModule1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> bar;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subModule2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> bar;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;</p>
<p><span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>I realized that it was not possible to refer to the properties of the precedent subModules within a subModule closure section (above the return statement), since the subModules are not an object literal, but a function that return an object.</p>
<h3>I was astonished that I cannot access the bigModule properties, but deeper from within another function:</h3>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> bigModule = undefined; <span class="co1">// used for the debugging</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// purpose of this article</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// to make sure it has never</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// already returned the object.</span><br />
<span class="kw2">var</span> bigModule = <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> foo;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subModule1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> bar;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;subModule1.method1() called&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subModule2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Error: bigModule has no properties //</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bigModule.<span class="me1">subModule1</span>.<span class="me1">method1</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method1: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// OK!, bigModule has already returned here //</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bigModule.<span class="me1">subModule1</span>.<span class="me1">method1</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method2: <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;</p>
<p><span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>Even if I can do some hacks (some ugly, some ok, depending of what I want to achieve) to make it possible to access the bigModule variable once returned, before the return statement of the subModule2 property, I&#8217;m still confused and wonder if that pattern of programmation is really powerful or is limited (e.g. nested function literals).</p>
<p>I&#8217;m going to study it and understand that behavior. It seems that a function literal won&#8217;t be executed until it stops stacking deeper function literals. </p>
<p>Any explications?</p>
]]></content:encoded>
			<wfw:commentRss>http://philrathe.com/2008/02/03/javascript-scope-and-sequence-execution-can-surprise-you/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The jQuery later &#8220;plugin&#8221;</title>
		<link>http://philrathe.com/2007/12/10/jquery-later-plugin/</link>
		<comments>http://philrathe.com/2007/12/10/jquery-later-plugin/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 21:16:05 +0000</pubDate>
		<dc:creator>Philippe Rathe</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://philrathe.com:8080/?p=7</guid>
		<description><![CDATA[ You can extend jQuery.fn by assigning a later function to it to avoid always defining a callback function just to use setTimeout. later returns the object that called the function, so it do not breaks the chainability magic in jQuery.
The later plugin for jQuery
 [ Thanks to  Douglas Crockford for his teaching ]
jQuery.fn.later [...]]]></description>
			<content:encoded><![CDATA[<p> You can extend <code>jQuery.fn</code> by assigning a <code>later</code> function to it to avoid always defining a callback function just to use <code>setTimeout</code>. <code>later</code> returns the object that called the function, so it do not breaks the chainability magic in jQuery.</p>
<h3>The later plugin for jQuery</h3>
<p><span style="font-size: 80%; color: #bbbbbb; font-style: italic"> [ Thanks to <a href="http://www.crockford.com/"> Douglas Crockford</a> for his teaching ]</span></p>
<div class="dean_ch" style="white-space: nowrap;">jQuery.<span class="me1">fn</span>.<span class="me1">later</span> = <span class="kw2">function</span><span class="br0">&#40;</span>msec, method<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> that = <span class="kw1">this</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> args = Array.<span class="me1">prototype</span>.<span class="me1">slice</span>.<span class="me1">apply</span><span class="br0">&#40;</span>arguments, <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> method === <span class="st0">&quot;string&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method = that<span class="br0">&#91;</span>method<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; setTimeout<span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method.<span class="me1">apply</span><span class="br0">&#40;</span>that, args<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>, msec<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> that;<br />
<span class="br0">&#125;</span>;</div>
<p>It was almost my first plugin, but that is too short to make it a plugin :-)</p>
<p>It is more likely a feature. Let&#8217;s see examples of how to use it.</p>
<h3>Samples of use for the later function</h3>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">// Show &quot;Saved!&quot; for 3 seconds</span><br />
$<span class="br0">&#40;</span><span class="st0">&#8216;#myid&#8217;</span><span class="br0">&#41;</span>.<span class="me1">before</span><span class="br0">&#40;</span><span class="st0">&quot;Saved!&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">prev</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">3000</span>, <span class="st0">&quot;hide&quot;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// Show &quot;Saved!&quot; for 3 seconds, hiding slowly</span><br />
$<span class="br0">&#40;</span><span class="st0">&#8216;#myid&#8217;</span><span class="br0">&#41;</span>.<span class="me1">before</span><span class="br0">&#40;</span><span class="st0">&quot;Saved!&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">prev</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">3000</span>, <span class="st0">&quot;hide&quot;</span>, <span class="st0">&quot;slow&quot;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// Chaining later &#8230;</span><br />
<span class="co1">// Change the appearance of the list for 4 seconds,</span><br />
<span class="co1">// &nbsp; show a &quot;saved&quot; notice before it</span><br />
<span class="co1">// &nbsp; and fade it slowly 3 seconds after if appears</span><br />
$<span class="br0">&#40;</span><span class="st0">&#8216;#list_id&#8217;</span><span class="br0">&#41;</span><br />
.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&quot;success&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">4000</span>, <span class="st0">&quot;removeClass&quot;</span>, <span class="st0">&quot;success&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">before</span><span class="br0">&#40;</span><span class="st0">&quot;Saved!&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">prev</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">7000</span>, <span class="st0">&quot;fadeOut&quot;</span>, <span class="st0">&quot;slow&quot;</span><span class="br0">&#41;</span>;</div>
<h3>When <strong>*not*</strong> to use it</h3>
<ul>
<li>If you want to call a jQuery function right after an effect has finished, use the jQuery callback instead</li>
<li>If you are unsure that the function called by <code>later</code> will be executed after the previous one has finished and that the order matters</li>
</ul>
<h3>When  to use it</h3>
<ul>
<li>If you can&#8217;t or don&#8217;t want to use the jQuery callback function</li>
<li>You would use the jQuery callback function just to use the <code>setTimeout</code> function to join two actions that are not dependent</li>
<li>When you can control the exact sequence of execution and you are sure it will be executed in that order</li>
</ul>
<p>I&#8217;ve post a small article on using the <code>later</code> method with your own object. <a href="http://philrathe.com/2007/12/8/later-is-better" title="later is better">Read it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://philrathe.com/2007/12/10/jquery-later-plugin/feed/</wfw:commentRss>
		</item>
		<item>
		<title>later is better</title>
		<link>http://philrathe.com/2007/12/08/later-is-better/</link>
		<comments>http://philrathe.com/2007/12/08/later-is-better/#comments</comments>
		<pubDate>Sat, 08 Dec 2007 21:00:49 +0000</pubDate>
		<dc:creator>Philippe Rathe</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://philrathe.com:8080/?p=6</guid>
		<description><![CDATA[I really enjoy hanging out at the YUI theater sometimes. There are very good conferences given mainly by the Yahoo! staff. I particularly like those one from Douglas Crockford, a Yahoo! JavaScript architect teach us very good stuff. In his &#8220;Advanced JavaScript&#8221; talk, he gives us a more OO solution to the setTimeout function. Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I really enjoy hanging out at the <a href="http://developer.yahoo.com/yui/theater/">YUI theater</a> sometimes. There are very good conferences given mainly by the Yahoo! staff. I particularly like those one from <a href="http://www.crockford.com/">Douglas Crockford</a>, a Yahoo! JavaScript architect teach us very good stuff. In his &#8220;Advanced JavaScript&#8221; talk, he gives us a more OO solution to the setTimeout function. Let&#8217;s look at the later function code</p>
<h3>The later function</h3>
<div class="dean_ch" style="white-space: nowrap;">Object.<span class="me1">prototype</span>.<span class="me1">later</span> = <span class="kw2">function</span><span class="br0">&#40;</span>msec, method<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> that = <span class="kw1">this</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> args = Array.<span class="me1">prototype</span>.<span class="me1">slice</span>.<span class="me1">apply</span><span class="br0">&#40;</span>arguments, <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> method === <span class="st0">&quot;string&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method = that<span class="br0">&#91;</span>method<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; setTimeout<span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; method.<span class="me1">apply</span><span class="br0">&#40;</span>that, args<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>, msec<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> that;<br />
<span class="br0">&#125;</span>;</div>
<h3> Examples of use</h3>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> duck = <span class="br0">&#123;</span><br />
&nbsp; &nbsp; say: <span class="kw2">function</span><span class="br0">&#40;</span>what<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>what<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; shout: <span class="kw2">function</span><span class="br0">&#40;</span>n, what<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i = <span class="nu0">0</span>; i &lt; n; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">say</span><span class="br0">&#40;</span>what.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
<p>duck.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">6000</span>, <span class="st0">&quot;say&quot;</span>, <span class="st0">&quot;Quack!&quot;</span><span class="br0">&#41;</span>.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">12000</span>, <span class="st0">&quot;shout&quot;</span>, <span class="nu0">3</span>, <span class="st0">&quot;Quack!&quot;</span><span class="br0">&#41;</span>;</div>
<p>Arguments [2..n] are the arguments to pass to the function</p>
<p>Since it extends <code>Object</code> itself, later will be available to every functions. <strong>That is a very bad idea to extend the Object prototype.</strong> Why? Because you don&#8217;t want to break code that is not yours, as with libraries or api. Looping over each properties of that <code>Object</code> is often the only way they have to accomplish their goal. We must never touch the <code>Object</code> prototype. I know, I b<br />
reak my code using that later function with jQuery.</p>
<p>If you do not use any code that&#8217;s not yours, you can augment <code>Object.prototype</code> otherwise you&#8217;ll have to find another way not to break other&#8217;s code. In order to use <code>later</code> you could extend your own object depending on the pattern you use for programming JavaScript. The following is an example of how you could use it.</p>
<h3>The later function in a simple pattern</h3>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Animal<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span></p>
<p>Animal.<span class="me1">prototype</span>.<span class="me1">later</span> = <span class="kw2">function</span><span class="br0">&#40;</span>msec, method<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">var</span> that = <span class="kw1">this</span>,<br />
&nbsp; &nbsp; &nbsp; args = Array.<span class="me1">prototype</span>.<span class="me1">slice</span>.<span class="me1">apply</span><span class="br0">&#40;</span>arguments, <span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> method === <span class="st0">&quot;string&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; method = that<span class="br0">&#91;</span>method<span class="br0">&#93;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; setTimeout<span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; method.<span class="me1">apply</span><span class="br0">&#40;</span>that, args<span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span>, msec<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">return</span> that;<br />
<span class="br0">&#125;</span>;</p>
<p>Animal.<span class="me1">prototype</span>.<span class="me1">say</span> = <span class="kw2">function</span><span class="br0">&#40;</span>what<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>what<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span>;</p>
<p>Animal.<span class="me1">prototype</span>.<span class="me1">shout</span> = <span class="kw2">function</span><span class="br0">&#40;</span>n, what<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i = <span class="nu0">0</span>; i &lt; n; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">say</span><span class="br0">&#40;</span>what.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
<p>duck = <span class="kw2">new</span> Animal<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
duck.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">6000</span>, <span class="st0">&quot;say&quot;</span>, <span class="st0">&quot;Quack!&quot;</span><span class="br0">&#41;</span>.<span class="me1">later</span><span class="br0">&#40;</span><span class="nu0">12000</span>, <span class="st0">&quot;shout&quot;</span>, <span class="nu0">3</span>, <span class="st0">&quot;Quack!&quot;</span><span class="br0">&#41;</span>;</div>
<p><strong>Update: </strong> How to use the <code>later function</code> through chaining in jQuery? <a href="http://philrathe.com/2007/12/10/jquery-later-plugin">Read my post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://philrathe.com/2007/12/08/later-is-better/feed/</wfw:commentRss>
		</item>
		<item>
		<title>jQuery + Rails ( Part 1 )</title>
		<link>http://philrathe.com/2007/12/06/jquery-rails-part-1/</link>
		<comments>http://philrathe.com/2007/12/06/jquery-rails-part-1/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 20:32:42 +0000</pubDate>
		<dc:creator>Philippe Rathe</dc:creator>
		
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://philrathe.com:8080/?p=5</guid>
		<description><![CDATA[This article is not really about jRails but about unobtrusive JavaScript in Rails using jQuery.
So I decided to switch to jQuery for my Ruby on Rails apps. I&#8217;m quite enthusiast and the whole thing about this turnover is that I figured out that it makes sense. There is few talks about it, some have made [...]]]></description>
			<content:encoded><![CDATA[<p><strong>This article is not really about jRails but about unobtrusive JavaScript in Rails using jQuery.</strong></p>
<p>So I decided to switch to jQuery for my Ruby on Rails apps. I&#8217;m quite enthusiast and the whole thing about this turnover is that I figured out that it makes sense. There is few talks about it, some have made the move. There is even a <a href="http://ennerchi.com/projects/jrails" title="jQuery on Rails">plugin</a> that will make almost all your helpers you wrote in ERB works without you need to do anything about it. Many of your RJS code will still work. You can look at which of those features will still work out-of-the-box <a href="http://ennerchi.com/projects/jrails#features">here</a>.</p>
<p>I&#8217;m speaking about replacing completely Prototype and Scriptaculous by jQuery here, got it?</p>
<p><a href="http://jquery.com/blog/2006/08/20/why-jquerys-philosophy-is-better/" title="http://jquery.com/blog/2006/08/20/why-jquerys-philosophy-is-better/"><br />
</a></p>
<blockquote cite="http://jquery.com/blog/2006/08/20/why-&lt;br&gt;&lt;/blockquote&gt; jquerys-philosophy-is-better/"><p><a href="http://jquery.com/blog/2006/08/20/why-jquerys-philosophy-is-better/" title="http://jquery.com/blog/2006/08/20/why-jquerys-philosophy-is-better/">In fact, the difference in sensibilities [between Prototype and jQuery] is very similar to the difference in sensibilities between Java and Ruby, so it&#8217;s ironic that the Rails community has embraced Prototype so completely</a></p></blockquote>
<h3>Why jQuery and Rails together makes sense?</h3>
<ul>
<li>jQuery has tons of plugins, just like Ruby on Rails</li>
<li>jQuery is completely unobtrusive (MVC is great, but for Web applications MVC + J is better)</li>
<li>javascript with jQuery is fun, Ruby is fun, Rails is fun</li>
<li>jQuery design philosophy rules</li>
<li>The community is big and active</li>
</ul>
<h2>Tutorial: An unobtrusive ajax sortable list updated live</h2>
<h3>Prerequisites to follow my example</h3>
<p>1) You might want ot create a new rails application. We will need Rails 2, in order to use the new &#8220;js.erb&#8221; template (those aren&#8217;t RJS, see further in this article)</p>
<p><strong>Tips: Do not use the <code>rails</code> binary from rails installed as a gem if you plan to upgrade to edge. In that case use the edge binary:</strong></p>
<div class="dean_ch" style="white-space: nowrap;">$ svn <span class="kw2">co</span> http://dev.rubyonrails.com/svn/rails/trunk rails<br />
$ ruby rails/railties/bin/rails foo<br />
$ <span class="kw2">mv</span> rails foo/vendor<br />
$ <span class="kw3">cd</span> foo</div>
<p>&#8230; and you&#8217;re on silly edge! :-)</p>
<p>2) Remove everything in <code>public/javascripts</code> except application.js and put the jQuery <a href="http://docs.jquery.com/Downloading_jQuery" title="download the source">file</a> there. Also we will u<br />
se the <a href="http://interface.eyecon.ro/">Interface</a> jQuery plugin. You can <a href="http://interface.eyecon.ro/download">download</a>   only the UI components you need. Here we&#8217;ll need the Sortable modul<br />
e. Put that downloaded file in your javascript folder too.</p>
<p>3) To simplify this example we will not protect ourself from forgery for now. I will give a solution to this in a further post. So in your <code>app/controllers/application.rb</code>, comment the following line</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#protect_from_forgery # :secret =&gt; &#8216;e46918532966a85b851ddeeeec50207d&#8217;</span></div>
<p>4) Update your config/database.yml if needed, create the development DB then start your server and jump into a first example.</p>
<p>We won&#8217;t need <a href="http://ennerchi.com/projects/jrails">jRails</a> for now. I could have install it and use the sortable helper that do exactly what the one from Scriptaculous do, but we will later need to<br />
update a list via Ajax and for that reason, we will use the Interface plugin to keep it updated live as new contacts get add by an Ajax form (look further post).</p>
<h3>Setup</h3>
<p>Let the scaffold generator generates everything we need for our contact resource</p>
<div class="dean_ch" style="white-space: nowrap;">$ ./script/generate scaffold contact email:string position:integer<br />
$ rake db:migrate</div>
<p>Point out your browser to http://localhost:3000/contacts to see if you don&#8217;t have any errors. Add up as many contacts as you want for testing purpose.</p>
<p>You now must add the needed JavaScripts files. Add those into <code>app/views/layouts/contacts.html.erb</code>. It should almost looks like this.</p>
<div class="dean_ch" style="white-space: nowrap;">&lt;%= javascript_include_tag &#8216;jquery.js&#8217; %&gt;<br />
&lt;%= javascript_include_tag &#8216;interface.js&#8217; %&gt;<br />
&lt;%= javascript_include_tag &#8216;application.js&#8217; %&gt;</div>
<p>Now open <code>app/controllers/contacts_controller.rb</code> and update the code in the index action to sort the contacts by their position.</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="re1">@contacts</span> = Contact.<span class="me1">find</span><span class="br0">&#40;</span><span class="re3">:all</span><span class="br0">&#41;</span>.<span class="me1">sort_by</span> <span class="br0">&#123;</span> |t| t.<span class="me1">position</span> <span class="br0">&#125;</span></div>
<h3>Create the list</h3>
<p>Now we will create the list for the index template. Don&#8217;t remove the existing code so we can keep the links to our actions. Add the code just add the following at the end of the <code>app/views/contacts/index.html.erb</code> file:</p>
<div class="dean_ch" style="white-space: nowrap;">
&lt;h1&gt;Unobtrusive Ajax Sortable List&lt;/h1&gt;<br />
&lt;ul id=&quot;this_id_is_just_needed&quot;&gt;<br />
&nbsp; &lt;% for contact in @contacts %&gt;<br />
&lt;li id=&quot;contact_&lt;%= contact.id %&gt;&quot; class=&quot;sortableitem&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;%= contact.email %&gt;&lt;/li&gt;<br />
&lt;% end %&gt;&lt;/ul&gt;<br />
&nbsp;</div>
<p><strong>Note: </strong> The &#8220;ul&#8221; element just need an id. Otherwise, some of the sortable functions won&#8217;t work.</p>
<h3>Making the list sortable</h3>
<p>It&#8217;s time to add behavior to our plain html. We will do this in <code>public/javascripts/application.js</code>. That is what we call unobtrusive JavaScript because we didn&#8217;t use any helper in our index templat<br />
e that generate javascript. jQuery is especially nice doing that because it makes it easy to select elements and apply behaviors on them. The following is the code I use to make it. I&#8217;ve DRY it out to make it e<br />
asier to maintain. It is fully commented, I let you read and I&#8217;ll explain after.</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="coMULTI">/* Waiting for the DOM */</span><br />
$<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;* The global object of the application.<br />
&nbsp; &nbsp; &nbsp;* It acts as a namespace to avoid collision<br />
&nbsp; &nbsp; &nbsp;* with any other code<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> MYAPP == <span class="st0">&quot;undefined&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> MYAPP = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;* This is our version of Interface Sortable.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* All Sortable options have predecedence<br />
&nbsp; &nbsp; &nbsp;* over the default set here.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* We don&#8217;t have to care about Ajax here other than<br />
&nbsp; &nbsp; &nbsp;* posting the serialized data to an url. Data have been<br />
&nbsp; &nbsp; &nbsp;* already serialized by Sortable before calling &quot;onchange&quot;.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* Ajax events should hook the post request before it is sent<br />
&nbsp; &nbsp; &nbsp;* in order to set the correct header and datatype.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param {Array || Element}<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;selector &nbsp;The css selector<br />
&nbsp; &nbsp; &nbsp;* @param {funct} &nbsp; fn &nbsp; &nbsp; &nbsp; &nbsp;The Sortable function<br />
&nbsp; &nbsp; &nbsp;* @param {object} &nbsp;opt &nbsp; &nbsp; &nbsp; The Sortable options<br />
&nbsp; &nbsp; &nbsp;* @param {String} &nbsp;url &nbsp; &nbsp; &nbsp; The url to post the data<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* It returns the selector to keep chainability<br />
&nbsp; &nbsp; &nbsp;* and being consitent with the jQuery philosophy.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; MYAPP.<span class="me1">sortable</span> = <span class="kw2">function</span> <span class="br0">&#40;</span>selector, fn, opt, url<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; opt = $.<span class="me1">extend</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onchange: <span class="kw2">function</span> <span class="br0">&#40;</span>changes<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$.<span class="me1">post</span><span class="br0">&#40;</span>url, changes<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">hash</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opacity: &nbsp; <span class="nu0">0.7</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fit: &nbsp;<span class="kw2">false</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accept: <span class="st0">&#8217;sortableitem&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activeclass : <span class="st0">&#8217;sortableactive&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hoverclass : <span class="st0">&#8217;sortablehover&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; helperclass : <span class="st0">&#8217;sortablehelper&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>, opt<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; fn.<span class="me1">call</span><span class="br0">&#40;</span>selector, opt<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> selector;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>;</p>
<p>&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;* Ajax event.<br />
&nbsp; &nbsp; &nbsp;* Called before sending any XHR.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @params {Event} &nbsp;evt &nbsp; &nbsp; &nbsp; The event<br />
&nbsp; &nbsp; &nbsp;* @params {XHR} &nbsp; &nbsp;request &nbsp; The XmlHTTPRequest<br />
&nbsp; &nbsp; &nbsp;* @params {object} settings &nbsp;Settings for the POST XHR<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ajaxSend</span><span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span>evt, request, settings<span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* If we want to keep the Rails convention where XHR should<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* respond to javascript request,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* then we must change the header accordingly.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The default header is XML.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Rails conventions for XML request are not for XHR.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">setRequestHeader</span><span class="br0">&#40;</span><span class="st0">&quot;Accept&quot;</span>, <span class="st0">&quot;text/javascript&quot;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Expected server response can be one of the following values<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* =&gt; &#8216;xml&#8217;, &#8217;script&#8217;, &#8216;json&#8217; or null<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* null means that we will handle the response ourselves.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* It is the default option.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Because the MVC convention in Rails,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* it is in the Views that behaviors are defined, not here.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Though we cannot use &#8216;json&#8217; nor &#8216;xml&#8217;, because the server<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* response should have instructions<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* and not contains only datas.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The response will have to be evaluated as JavaScript.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; settings.<span class="me1">dataType</span> = <span class="kw2">null</span>;</p>
<p>&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;* Ajax event.<br />
&nbsp; &nbsp; &nbsp;* Called when the server response is successful<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* The server response is plain JavaScript that must be evaluated<br />
&nbsp; &nbsp; &nbsp;* because we set the dataType to null<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @params {Event} &nbsp;evt &nbsp; &nbsp; &nbsp; The event<br />
&nbsp; &nbsp; &nbsp;* @params {XHR} &nbsp; &nbsp;request &nbsp; The XmlHTTPRequest<br />
&nbsp; &nbsp; &nbsp;* @params {object} settings &nbsp;Settings for the POST XHR<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ajaxSuccess</span><span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span>evt, request, settings<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">eval</span><span class="br0">&#40;</span>request.<span class="me1">responseText</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* The flash messages and code could be in the server response<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* and/or it could be here in the callback too, but the latter<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* is more generic.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#flash-notice&quot;</span><span class="br0">&#41;</span>.<span class="me1">text</span><span class="br0">&#40;</span><span class="st0">&quot;Success&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;* Ajax event.<br />
&nbsp; &nbsp; &nbsp;* Called when the server response isn&#8217;t successful.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* It&#8217;s up to you to decide how to manage this.<br />
&nbsp; &nbsp; &nbsp;* In our case, the server error message is:<br />
&nbsp; &nbsp; &nbsp;* &nbsp; ajaxOptions.responseText<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ajaxError</span><span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span>XMLHttpRequest, ajaxOptions, thrownError<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;* This is where we make the list sortable.<br />
&nbsp; &nbsp; &nbsp;* Sortable options will overide the defaults.<br />
&nbsp; &nbsp; &nbsp;* We can chain that function call as we would do<br />
&nbsp; &nbsp; &nbsp;* with the jQuery object.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* That line could have been something like<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp;$(&quot;ul&quot;).Sortable({ &#8230; })<br />
&nbsp; &nbsp; &nbsp;* But I prefer that way, it&#8217;s more readable<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; MYAPP.<span class="me1">sortable</span><span class="br0">&#40;</span>$<span class="br0">&#40;</span><span class="st0">&quot;ul&quot;</span><span class="br0">&#41;</span>, $.<span class="me1">fn</span>.<span class="me1">Sortable</span>, <span class="br0">&#123;</span>opacity: &nbsp; <span class="nu0">0.5</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;/contacts/order&quot;</span><span class="br0">&#41;</span>;</p>
<p><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
<p><strong>Explications</strong></p>
<p>You see that my functions are wrapped in the <code>MYAPP</code> global object. I always to that choosing a better name though. <code>MYAPP.sortable</code> (<strong>The last line of the previous code listing</strong>) is the function that will call the real <code>jQuery.Sortable</code> function. I&#8217;ve written it like that, so we don&#8217;t repeat some of the options, like <code>onchange</code> that won&#8217;t change. All of the options can be overwrite at the <code>MYAPP.<br />
sortable</code> call within the arguments. Check out the <a href="http://interface.eyecon.ro/docs/sort">official Interface documentation</a> for those options.</p>
<h3>Hooking Ajax event</h3>
<p>The good thing is that we can hook some Ajax events like <code>ajaxSuccess</code> or <code>ajaxSend</code> to define the behavior of our unobtrusive Ajax application. We have define it once we are done for all our Rails Ajax application. It&#8217;s completely unobtrusive and DRY.</p>
<h3>Defining the order action</h3>
<p>In our call on <strong>line 136</strong> we set the post url for our list. The action order isn&#8217;t yet define in our controller, so we must do it.</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#</span><br />
<span class="co1"># POST /contacts/order.js</span><br />
<span class="co1">#</span><br />
<span class="kw1">def</span> order</p>
<p>&nbsp; <span class="co1"># An array of ids of contact of their new order</span><br />
&nbsp; <span class="re1">@ids</span> = params.<span class="me1">values</span>.<span class="kw3">select</span> <span class="br0">&#123;</span> |v| v.<span class="me1">is_a</span>?<span class="br0">&#40;</span><span class="kw3">Array</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>.<span class="me1">first</span></p>
<p>&nbsp; <span class="co1"># To send to the view, to display the new order</span><br />
&nbsp; <span class="re1">@order</span> = <span class="re1">@ids</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;, &quot;</span><span class="br0">&#41;</span></p>
<p>&nbsp; i = <span class="nu0">1</span><br />
&nbsp; <span class="re1">@ids</span>.<span class="me1">each</span> <span class="kw1">do</span> |id|<br />
&nbsp; &nbsp; <span class="co1"># get the last longuest number ending a string</span><br />
&nbsp; &nbsp; <span class="co1"># and take it as the contact id</span><br />
&nbsp; &nbsp; c = Contact.<span class="me1">find</span><span class="br0">&#40;</span>id.<span class="kw3">gsub</span><span class="br0">&#40;</span>/^.<span class="me1">*</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="nu0">-9</span><span class="br0">&#93;</span>+<span class="br0">&#41;</span>$/, <span class="st0">&quot;<span class="es0">\\</span>1&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> c.<span class="me1">position</span> != i<br />
&nbsp; &nbsp; &nbsp; c.<span class="me1">position</span> = i;<br />
&nbsp; &nbsp; &nbsp; c.<span class="me1">save</span><br />
&nbsp; &nbsp; <span class="kw1">end</span><br />
&nbsp; &nbsp; i += <span class="nu0">1</span>;<br />
&nbsp; <span class="kw1">end</span></p>
<p>&nbsp; respond_to <span class="kw1">do</span> |format|<br />
&nbsp; &nbsp; <span class="kw3">format</span>.<span class="me1">js</span><br />
&nbsp; <span class="kw1">end</span><br />
<span class="kw1">end</span></div>
<h3>Building the server response with POJS + ERB</h3>
<p>POJS stands for Plain Old JavaScript. I told you that we won&#8217;t use RJS template. We could if we&#8217;ve wanted, install the jRails plugin and<br />
use the RJS-style template to generate the JavaScript, just as if it were RJS, because jRails provide the JavaScript helpers needed to replace Prototype / Scriptaculous code by jQuery code. But we won&#8217;t write R<br />
JS-like code. Instead of writing ruby code in a &#8220;.js.rjs&#8221; file, we will write JavaScript with embedded Ruby in a &#8220;.js.erb&#8221; file. Since Rails 2.0, you can do it. Before Rails 2.0, the <a href="http://www.danwebb%3Cbr%3E%3C/a%3E.net/2006/11/24/minusmor-released">minus_mor plugin</a> were needed to do so.</p>
<p>So create the file <code>app/views/contacts/order.js.erb</code> and put the server response code in it:</p>
<div class="dean_ch" style="white-space: nowrap;">$<span class="br0">&#40;</span><span class="st0">&quot;ul&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">after</span><span class="br0">&#40;</span><span class="st0">&quot;&lt;span&gt;New order is: &lt;%= @order %&gt;&lt;/span&gt;&quot;</span><span class="br0">&#41;</span><br />
.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="st0">&quot;slow&quot;</span>, <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> that = <span class="kw1">this</span>;<br />
&nbsp; &nbsp; setTimeout<span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span>that<span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="st0">&quot;slow&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>, <span class="nu0">4000</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
<p>We are done! This code just append the new ordered list ids in the order they have been received by the server. It create a new element right after the list with some show and hide fade effects.</p>
<p>I&#8217;m quite new to Rails and jQuery and I will appreciate feedback on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://philrathe.com/2007/12/06/jquery-rails-part-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>svndo to do less svn typing</title>
		<link>http://philrathe.com/2007/11/27/svndo-to-do-less-svn-typing/</link>
		<comments>http://philrathe.com/2007/11/27/svndo-to-do-less-svn-typing/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 01:19:42 +0000</pubDate>
		<dc:creator>Philippe Rathe</dc:creator>
		
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://philrathe.com:8080/?p=4</guid>
		<description><![CDATA[I hack a very small ruby script that allow me to apply any command to my files that I use to manage with the Subversion status subcommand.
Let&#8217;s go with an example.
I have made an error deleting files under revision with the unix rm instead of using the svn rm command.
My status now looks like this:
$ [...]]]></description>
			<content:encoded><![CDATA[<p>I hack a very small ruby script that allow me to apply any command to my files that I use to manage with the Subversion status subcommand.</p>
<p>Let&#8217;s go with an example.<br />
I have made an error deleting files under revision with the unix <code>rm</code> instead of using the <code>svn rm</code> command.</p>
<p>My status now looks like this:</p>
<div class="dean_ch" style="white-space: nowrap;">$ svn st<br />
! &nbsp; &nbsp; &nbsp;db/migrate/015_create_tasks_and_their_relationships.rb<br />
! &nbsp; &nbsp; &nbsp;db/migrate/010_create_users.rb<br />
! &nbsp; &nbsp; &nbsp;db/migrate/020_create_categories_and_their_relationships.rb<br />
! &nbsp; &nbsp; &nbsp;vendor/plugins/exception_notification<br />
! &nbsp; &nbsp; &nbsp;README<br />
$</div>
<p>Subversion &#8216;!&#8217; status means that the file is missing, but is in revision. We need to do a <code>svn rm</code> on each of those files.</p>
<p>That&#8217;s where <code>svndo</code> come to the rescue. <code>svndo</code> takes as first argument the status reported by the <code>svn st</code> command (&#8217;!&#8217; in our example). The arguments 2..n are the command which will be executed on the files matching the giving status.</p>
<p>Let&#8217;s try it</p>
<div class="dean_ch" style="white-space: nowrap;">$ svndo ! svn <span class="kw2">rm</span><br />
svn <span class="kw2">rm</span> db/migrate/015_create_tasks_and_their_relationships.rb<br />
svn <span class="kw2">rm</span> db/migrate/010_create_users.rb<br />
svn <span class="kw2">rm</span> db/migrate/020_create_categories_and_their_relationships.rb<br />
svn <span class="kw2">rm</span> vendor/plugins/exception_notification<br />
svn <span class="kw2">rm</span> README<br />
$</div>
<p>What do we got here? <code>svndo</code> shows us each files matching the &#8216;!&#8217; status and the status is replace by the command that we pass as the last the arguments (2..n). <strong>Those commands are not executed until you pipe it to a shell interpreter.</strong></p>
<p>Let&#8217;s try it!</p>
<div class="dean_ch" style="white-space: nowrap;">$ svndo ! svn <span class="kw2">rm</span> | <span class="kw2">sh</span><br />
D &nbsp; &nbsp; &nbsp; &nbsp; db/migrate/015_create_tasks_and_their_relationships.rb<br />
D &nbsp; &nbsp; &nbsp; &nbsp; db/migrate/010_create_users.rb<br />
D &nbsp; &nbsp; &nbsp; &nbsp; db/migrate/020_create_categories_and_their_relationships.rb<br />
D &nbsp; &nbsp; &nbsp; &nbsp; vendor/plugins/exception_notification<br />
D &nbsp; &nbsp; &nbsp; &nbsp; README<br />
$</div>
<p>That&#8217;s it! Subversion has deleted your files from the current revision. I found it very useful to add new files and revert changes. You can use <code>svndo</code> to match every svn status code <strong>e.g. M   !   ?   ~   A   D   and so.</strong> You must escape those who will be interpreted by your shell.</p>
<p>I got now problem to use the special bash character <strong>?</strong> and <strong>!</strong> as the first argument. I only had to do an alias to match most effectively the <strong>~</strong> character, because it&#8217;s interpreted by the shell and cannot be pass as-is as an argument (You must escape it)</p>
<div class="dean_ch" style="white-space: nowrap;">$ <span class="kw3">alias</span> svndo~=<span class="st0">&#8217;svndo ~&#8217;</span></div>
<p>So I can now only use</p>
<div class="dean_ch" style="white-space: nowrap;">$ svndo~ some <span class="kw3">command</span></div>
<p>instead of</p>
<div class="dean_ch" style="white-space: nowrap;">$ svndo \~ some <span class="kw3">command</span></div>
<p><strong>*** Update: After <a href="http://macournoyer.wordpress.com/">Marc-André</a> has posted a comment notifying me the script wasn&#8217;t working, I realize that I&#8217;ve messed up with regular expressions&#8217; backslashes when posting the code. Thank you Marc-André for your refactoring, your code looks like Ruby code, while mine looks a bit like bash. So I&#8217;ve been inspired by you to post that new code. It do exactly what it was doing before, but now you can use the $ character in your command to be replace by the file name who the status is associated with.</strong></p>
<p>It becomes now more flexible.</p>
<div class="dean_ch" style="white-space: nowrap;">$ svndo ? <span class="kw2">mv</span> $ /somewhereelse</div>
<p><strong>That is the svndo script I was talking about.</strong></p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#!/usr/bin/ruby -w</span></p>
<p>status, *cmd = ARGV</p>
<p><span class="st0">`svn st`</span>.<span class="me1">each</span> <span class="kw1">do</span> |line|<br />
&nbsp; <span class="kw1">if</span> matches = /^<span class="co1">#{Regexp.escape(status)}\s+(.*)$/.match(line)</span><br />
&nbsp; &nbsp; tmp = cmd.<span class="me1">map</span> <span class="br0">&#123;</span>|c| c.<span class="kw3">gsub</span><span class="br0">&#40;</span>/\$/,matches<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; tmp &lt;&lt; matches<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="kw1">if</span> tmp == cmd<br />
&nbsp; &nbsp; <span class="kw3">puts</span> tmp.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&#8216; &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="kw1">end</span><br />
<span class="kw1">end</span></div>
<p>Refactor this code at: <a href="http://refactormycode.com/codes/186-script-for-subversion-status-manipulation">refactormycode.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://philrathe.com/2007/11/27/svndo-to-do-less-svn-typing/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
