<?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>Javascript Kata &#187; how to</title>
	<atom:link href="http://www.javascriptkata.com/category/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Tue, 15 Jun 2010 14:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to de-anonymize your anonymous functions</title>
		<link>http://www.javascriptkata.com/2010/05/19/how-to-de-anonymize-your-anonymous-functions/</link>
		<comments>http://www.javascriptkata.com/2010/05/19/how-to-de-anonymize-your-anonymous-functions/#comments</comments>
		<pubDate>Wed, 19 May 2010 17:31:52 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=246</guid>
		<description><![CDATA[First of all, de-anonymized functions are called <strong>named functions</strong> and they look a lot like "regular" functions (in fact, they are).]]></description>
			<content:encoded><![CDATA[<p>First of all, de-anonymized functions are called <strong>named functions</strong> and they look a lot like &#8220;regular&#8221; functions (in fact, they are).</p>
<h3>Why should I de-anonymize?</h3>
<p>For <strong>debugging</strong>. Instead of having a stack trace filled with <strong>?()</strong> (representing an anonymous function), it is nicely printed and easier to follow.</p>
<p>For <strong>self-reference</strong>. Example, when you want an anonymous function to recall itself recursively, you would have to call <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/Arguments/Callee" target="_blank">arguments.callee</a>. It&#8217;s odd <del datetime="2010-05-20T14:23:32+00:00">and it&#8217;s <strong>deprecated</strong> anyway</del> (people pointed to me that it&#8217;s not really deprecated but it throws an error in <a href="http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/">ECMAScript 5 strict</a>).</p>
<h3>Stack trace of anonymous functions&#8230;</h3>
<p>If you run this in your <a href="http://getfirebug.com">firebug console</a> (every javascripter should have firebug).</p>
<p><script src="http://gist.github.com/406349.js?file=anonymous+functions.js"></script></p>
<p>You get a stack trace that looks like this :<br />
func()<br />
func()<br />
func()</p>
<h3>&#8230; versus stack trace of named functions</h3>
<p><script src="http://gist.github.com/406354.js?file=named+functions.js"></script></p>
<p>You get a stack trace that looks like this :<br />
f1()<br />
f2()<br />
f3()</p>
<p>As you can see, it&#8217;s a lot easier to debug and to understand who&#8217;s calling who.</p>
<p><strong>Warning</strong> : There&#8217;s a <a href="http://yura.thinkweb2.com/named-function-expressions/#jscript-bugs">bug in IE</a> with named functions.</p>
<h3>Recursive anonymous function</h3>
<p>Let&#8217;s create a recursive anonymous function that counts to 10.</p>
<p><script src="http://gist.github.com/406360.js?file=recursive+count+to+10+anonymous.js"></script></p>
<p>And let&#8217;s create it but <strong>using a named function</strong>.</p>
<p><script src="http://gist.github.com/406362.js?file=recursive+count+to+10+with+named+functions.js"></script></p>
<p>Once again, the code is much clearer and it&#8217;s not using a deprecated element.</p>
<h3>A note about closures to advanced javascripters</h3>
<p>I just wanted to say that in the last example, you could have written this :<br />
<script src="http://gist.github.com/406379.js?file=CountTo10closure.js"></script></p>
<p>As you can see at <strong>line 4</strong>, I call the <em>count</em> function which is not the name of the function but the variable to which the function is assigned. It works, but it may not be the best thing to do. You can learn more about closure on that <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/">previous post</a> and I&#8217;ll write more about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/05/19/how-to-de-anonymize-your-anonymous-functions/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to write a simple undo system for your app</title>
		<link>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/</link>
		<comments>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 14:20:39 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=188</guid>
		<description><![CDATA[I really like undos. If I could undo that that last beer... Unfortunately, I can't. But, <strong>I can offer undo to users of my application</strong>. ]]></description>
			<content:encoded><![CDATA[<p>I really like undos. If I could undo that that last beer&#8230; Unfortunately, I can&#8217;t. But, <strong>I can offer undo to users of my application</strong>. </p>
<p>When I thought about undo before, I thought about a complicated Rails plugin that would keep an anonymous ID linking to any table in the database with an &#8220;action&#8221; field that would contain the action to undo. Pretty complicated stuff for something as simple.</p>
<h3>The javascript solution</h3>
<p>One day while I was writing some javascript, the solution struck me : <strong>I could handle this completely with javascript</strong>. The basic idea is that whenever an action is completed, I can save its undo function in an array and call it whenever I need it.</p>
<p>Example, if I&#8217;m saving a user in a database with Ajax :</p>
<ol>
<li>I send the data to server to save a new user</li>
<li>The server returns the ID of the new user</li>
<li>I create a function with an ajax request that will send a delete of this user to the server</li>
<li>I add the previous function to a stack of <em>undo</em> functions to be able to call it later</li>
</ol>
<p>That, when I call that last function, it will undo that user creation without having to keep it in memory server-side. </p>
<h3>Introducing jsKata.undo</h3>
<p>I wrote a little something called <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.undo.js">jsKata.undo on GitHub</a> that contains the logic describe earlier. It&#8217;s quite simple but it may grow over time. It has no requirement, not even jQuery. It&#8217;s very simple to use.</p>
<p>The <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.undo.textile">complete doc</a> is on GitHub.</p>
<h4>1. Adding an action that can you can undo</h4>
<p>I&#8217;ll use the &#8220;add a user&#8221; example with jQuery.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">post</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;http://yoursite.com/users/new&quot;</span>, <span class="co1">// The url to add a user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span><span class="kw3">name</span>:<span class="st0">&quot;John Boucher&quot;</span><span class="br0">&#125;</span>, <span class="co1">// The name of the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>newUser, textStatus, XMLHttpRequest<span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// This is called when the post is over</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> newUserId = newUser.<span class="me1">id</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We begin by creating a function to delete the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> undoNewUser = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; $.<span class="me1">post</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;http://yoursite.com/users/delete&quot;</span>, <span class="co1">// The url to delete a user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>id:newUserId<span class="br0">&#125;</span> <span class="co1">// The ID of the new user to delete</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We add the undo function to the stack</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; jskataUndo.<span class="me1">push</span><span class="br0">&#40;</span>undoNewUser<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h4>2. Undo the last action you made</h4>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">undo</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h4>3. Events</h4>
<p>Each time there&#8217;s a change, an <em>onChange</em> event is called. To assign yours, simply write :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">onChange</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Show the undo button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#undoButton&quot;</span><span class="br0">&#41;</span>.<span class="me1">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>There&#8217;s also an <em>onEmpty</em> event that is called when the stack of undoable actions is empty.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">onEmpty</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Hide the undo button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#undoButton&quot;</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>Complete demo</h3>
<p>There is a <a href="http://dsimard.github.com/jskata_examples/undo">complete demo on GitHub</a> as well as the <a href="http://github.com/dsimard/jskata_examples/blob/gh-pages/undo.html">HTML and Javascript</a> code for it. Take a look!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How to have jQuery and prototype.js in the same project</title>
		<link>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/</link>
		<comments>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 11:02:41 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[librairies]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/</guid>
		<description><![CDATA[I&#8217;m a big fan of jQuery. This librarie is just the best and simplest one around. I really noticed it when I wanted to get rid of jQuery in TimmyOnTime and try to use prototype.js instead, just to be more &#8220;rails-oriented&#8221; (that&#8217;s a pretty lame excuse don&#8217;t you think?) Why I didn&#8217;t like prototype.js There [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of <a href="http://jquery.com/" target="_blank">jQuery</a>. This librarie is just the <strong>best</strong> and <strong>simplest</strong> one around. I really noticed it when I wanted to get rid of jQuery in <a href="http://timmyontime.com/" target="_blank">TimmyOnTime</a> and try to use <a href="http://www.prototypejs.org/" target="_blank">prototype.js</a> instead, just to be more &#8220;rails-oriented&#8221; (that&#8217;s a pretty lame excuse don&#8217;t you think?)</p>
<h3>Why I didn&#8217;t like prototype.js</h3>
<p>There was not a lot of javascript written for the project so I thought that getting rid of jQuery and using prototype.js would be easy. It turned out to be hell on earth. <strong>Prototype.js made me feel like I was back in the 90s writing C++</strong>. A simple click event turns out to be a awful lot of ugly code with ugly function names.</p>
<p>Example, <strong>if I wanted to show the content of a DIV I clicked</strong> in prototype.js, I would use things like <a href="http://www.prototypejs.org/api/event/observe" target="_blank">Event.observe</a>, <a href="http://www.prototypejs.org/api/function/bindAsEventListener" target="_blank">bindAsEventListener</a>, a mix of native DOM element and prototype.js element, and worst of all, an <a href="http://www.prototypejs.org/api">unintelligible documentation</a>.</p>
<p>jQuery as a complete different way of doing it in a single easy-to-understand line :<br />
<em>$(&#8220;#a_div&#8221;).click(function() { alert($(this).text()); });</em>.<br />
As simple as that!</p>
<h3>The problem</h3>
<p>I wanted to use some interface element in Rails that requires prototype.js so I had to have both librairies. The problem is that there&#8217;s a conflict between them. Prototype.js doesn&#8217;t seem to give a damn about it but jQuery is nicer with you. It offers a <a href="http://docs.jquery.com/Core/jQuery.noConflict" target="_blank">noConflict</a> method.</p>
<p><strong>With Rails</strong>, prototype.js is the default librairie so to override this, you could do</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;%= javascript_include_tag &quot;prototype&quot;, &quot;jquery&quot; %&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;$j = jQuery.noConflict();&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>This way, you will have to use <strong>$j</strong> instead of <strong>$</strong> to call jQuery.<strong>$</strong> would still call prototype.</p>
<p>Another thing you could do is to use <a href="http://ennerchi.com/projects/jrails" target="_blank">jQuery on Rails</a> and don&#8217;t give a damn about scriptaculous. I personally prefer to use librairies that are fully tested instead of using a &#8220;by-pass&#8221; that could break my code. It&#8217;s your choice&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
