<?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>JavascriptKata &#187; black belt</title>
	<atom:link href="http://www.javascriptkata.com/category/level/black-belt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>helping you with javascript since 2007</description>
	<lastBuildDate>Wed, 14 Sep 2011 14:07:48 +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>How to make sure undefined is not defined</title>
		<link>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-make-sure-undefined-is-not-defined</link>
		<comments>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 13:59:30 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=365</guid>
		<description><![CDATA[Think about it, you write a plugin or a library (let&#8217;s name it jsKata) and your code contains checks to see if certain things are undefined. Here&#8217;s an example : if (obj.name === undefined) { &#160; obj.name = &#34;This object has no name&#34;; } This code works well until you include another script (found on [...]]]></description>
			<content:encoded><![CDATA[<p>Think about it, you write a plugin or a library (let&#8217;s name it <a href="http://github.com/dsimard/jskata" target="_blank">jsKata</a>) and your code contains checks to see if certain things are <em>undefined</em>.</p>
<p>Here&#8217;s an example :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">if (obj.name === undefined) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; obj.name = &quot;This object has no name&quot;;
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>This code works well until you include another script (found on an obscure website) and it breaks your previous code. By debugging the code, you see the value of <em>undefined</em> is no more <em>undefined</em> but <em>false</em>.</p>
<p>Yes, I know that you thought it was impossible to define <em>undefined</em> but it is, you just have to write <em>undefined = false</em>.</p>
<h3>Redefine undefined</h3>
<p>There&#8217;s two way of redefining undefined and they both use <a href="http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/" title="Self-invoking functions explained line by line" target="_blank">self-invoking functions</a>.</p>
<h4>Method 1 : Scope your code</h4>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">(function(undefined) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; /* your complete code here */
</div>
</li>
<li class="li1">
<div class="de1">})();</div>
</li>
</ol>
</div>
<p>As you see, the self-invoking function has one parameter named <em>undefined</em>. At line 3, the self-invoking functions is called <strong>without any parameter</strong>. It results that the parameter has the value <em>undefined</em> (or it is defined to <em>undefined</em> if you prefer).</p>
<p>Take a look at this javascript :<br />
<script src="https://gist.github.com/1209870.js?file=fiddle.js"></script></p>
<p>It will display <em>false</em> and then display <em>undefined</em> even if undefined was globally defined at first.</p>
<p>This is the HTML that goes with it :<br />
<script src="https://gist.github.com/1209870.js?file=fiddle.html"></script></p>
<p>You can also execute it <a href="http://jsfiddle.net/gh/gist/jquery/1.6/1209870/" target="_blank">here</a>.</p>
<h4>Method 2 : Globally redefine undefined</h4>
<p>There&#8217;s another method but I don&#8217;t like it that much. It redefines undefined but nothing protects it from being badly redefined in another chunk of code.</p>
<p><script src="https://gist.github.com/1213798.js?file=fiddle.js"></script></p>
<p><em>undefined</em> is a global variable accessible using the global <em>window</em> object. Line 2 defines <em>undefined</em> and line 3 display the value of <em>undefined</em> as <em>false</em>. On line 4, I use a <a href="http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/" title="Self-invoking functions explained line by line" target="_blank">self-invoking function</a> as method 1 does but now, I define the global <em>undefined</em> variable through the global <em>window</em> object.</p>
<p>The HTML of this example :<br />
<script src="https://gist.github.com/1213798.js?file=fiddle.js"></script></p>
<p>You can also execute it on <a href="http://jsfiddle.net/gh/gist/jquery/1.6/1213798/" target="_blank">jsFiddle</a>.</p>
<h3>Other methods</h3>
<p>Some commentors pointed me other ways of doing it.</p>
<p>You can use <strong>typeof</strong> :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">typeof undefined == &quot;undefined&quot;</div>
</li>
</ol>
</div>
<p>Or you could use <strong>void(0)</strong>, it always returns <em>undefined</em></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">undefined === void(0) // this returns true</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Self-invoking functions explained line by line</title>
		<link>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=self-invoking-functions-explained-line-by-line</link>
		<comments>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 02:16:04 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=343</guid>
		<description><![CDATA[I never knew it was possible to love a syntax but since, I fell in love with the self-invoking function which can be summarized like this : (function(){})(). Yes this is a valid syntax though it doesn&#8217;t do much in this form (it creates a function that does nothing and call it). Some people call [...]]]></description>
			<content:encoded><![CDATA[<p>I never knew it was possible to love a syntax but since, I fell in love with the <strong>self-invoking function</strong> which can be summarized like this : <em>(function(){})()</em>. Yes this is a valid syntax though it doesn&#8217;t do much in this form (it creates a function that does nothing and call it).</p>
<p>Some people call it <em>self-invocation</em> or <em>self-executing</em> but I don&#8217;t think it has an official name.</p>
<h3>Let&#8217;s break it down</h3>
<p>It will become clearer if I execute some code and write it on more than one line :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; console.log(&quot;this line is called&quot;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li2">
<div class="de2">)
</div>
</li>
<li class="li1">
<div class="de1">();</div>
</li>
</ol>
</div>
<p><strong>Line 1 : (</strong><br />
This first parenthesis is used as <em>grouping operator</em>. Read more <a href="http://kangax.github.com/nfe/" target="_blank">here</a> and <a href="http://rx4ajax-jscore.com/ecmacore/more/express.html#group" target="_blank">here</a>.</p>
<p><strong>Line 2 : function() {</strong><br />
This create an anonymous function. I could write something like <strong>function doSomething() {</strong> but in the case of a self-invoking function, this is useless because the function is in its own little scope (line 1) and can&#8217;t be called from outside of it.</p>
<p><strong>Line 3 : console.log(&#8220;this line is called&#8221;);</strong><br />
This is the code that will be invoked.</p>
<p><strong>Line 4 : }</strong><br />
This clauses the function opened at line 2.</p>
<p><strong>Line 5 : )</strong><br />
Clauses the parens opened at line 1.</p>
<p><strong>Line 6 : ();</strong><br />
We call the function created at line 2 and returned within the scope from line 1 to 4.</p>
<p>Let&#8217;s write it in a more compact syntax :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">(function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; console.log(&quot;this line is called&quot;);
</div>
</li>
<li class="li1">
<div class="de1">})()</div>
</li>
</ol>
</div>
<p>See my post about <a href="http://www.javascriptkata.com/2011/09/05/creating-namespaces-with-self-invoking-functions/" title="Creating namespaces with self-invoking functions">using self-invoking functions to create namespaces</a>.</p>
<p>Don&#8217;t forget to <a href="http://eepurl.com/fmsZA">subscribe to my newsletter</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>NoFreeze : a library that avoids freezing in javascript</title>
		<link>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nofreeze-a-library-that-avoids-freezing-in-javascript</link>
		<comments>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 19:47:40 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[jskata]]></category>
		<category><![CDATA[libraries]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=274</guid>
		<description><![CDATA[The <a href="http://support.mozilla.com/en-us/kb/Warning+Unresponsive+script" target="_blank">unresponsive warning</a> happens a couple of time a year but every time, it's frustrating. It prompts the message "<em>A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete</em>".]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://support.mozilla.com/en-us/kb/Warning+Unresponsive+script" target="_blank">unresponsive warning</a> happens a couple of time a year but every time, it&#8217;s frustrating. It prompts the message &#8220;<em>A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete</em>&#8220;.</p>
<p>I <a href="http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/">launched jskata</a> not a long time ago and mostly talked about the <a href="http://dsimard.github.com/jskata/undo.html">undo</a> feature. Now, I will talk about the jskata <strong>NoFreeze</strong> library to <strong>avoid freezing by splitting long processes into smaller ones</strong>.</p>
<p>You can look at the <a href="http://dsimard.github.com/jskata/nofreeze.html" target="_blank">demo</a>, the <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.nofreeze.textile#readme" target="_blank">documentation</a> or the <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.nofreeze.js" target="_blank">source</a>.</p>
<h3>Old plain loop</h3>
<p>This script loops the <em>index</em> variable from 0 to 1000000 and writes the current value in <em>document.title</em>. Unfortunately, it will freeze the page until the whole process is done.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> index = <span class="nu0">0</span>; <span class="co1">// index</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span><span class="br0">&#40;</span>index = <span class="nu0">0</span>; index &lt;= <span class="nu0">1000000</span>; index++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; document.<span class="me1">title</span> = index;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How to write a responsive for</h3>
<p>I&#8217;m using <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.nofreeze.js" target="_blank">jsKata.nofreeze</a> library to avoid the freeze. This script will do the same as above. But, the <strong>page will remain responsive</strong> during the whole process.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> index = <span class="nu0">0</span>; <span class="co1">// index</span></div>
</li>
<li class="li1">
<div class="de1">jsKata.<span class="me1">nofreeze</span>.<span class="me1">forloop</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// the condition</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> index &lt;= <span class="nu0">1000000</span>; &nbsp;<span class="br0">&#125;</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// the incrementor</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> index++; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// this is what will be executed</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> fct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; document.<span class="me1">title</span> = index;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>It uses <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/">closures</a> to keep a resemblance to a good ol&#8217; <em>for</em> loop.</p>
<h3>Other loops : infinite and each</h3>
<p>There are two other loops available. <strong>Infinite</strong> will loop indefinitely until you stop it. <strong>Each</strong> will loop through the properties of an object and loop in each one.</p>
<p>Take a look at the <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.nofreeze.textile" target="_blank">documentation</a> to know more about them.</p>
<h3>How to stop a process</h3>
<p>As you see, every functions (forloop, infinite and each) return an object containing a <em>stop</em> function. So to stop a single process, just call it.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> loop = jsKata.<span class="me1">nofreeze</span>.<span class="me1">infinite</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><span class="co1">//do nothing});</span></div>
</li>
<li class="li1">
<div class="de1">loop.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>If you want to <strong>stop all the processes of a page</strong>, make this call :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">nofreeze</span>.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h3>More to come&#8230;</h3>
<p>There&#8217;s much more you can do with that library but I wanted to keep it simple for this post. Next, I&#8217;ll talk about multi-process and other kinds of loop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<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/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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>dsimard</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>7</slash:comments>
		</item>
		<item>
		<title>An undo/redo library for your app</title>
		<link>http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=an-undoredo-library-for-your-app</link>
		<comments>http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 20:26:00 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[jskata]]></category>
		<category><![CDATA[libraries]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=206</guid>
		<description><![CDATA[I wrote about undo before and I pushed it farther. What about an undo/redo system? I extended the v.01 of the undo and I now handle undo and redo in the same object. Annoucing redo in jskata.undo I made a new version of jskata.undo still hosted on GitHub. If you look closer, it is now [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote about <a href="http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/">undo</a> before and I pushed it farther. What about an <strong>undo/redo system</strong>? I extended the <a href="http://github.com/dsimard/jskataUndo/blob/v0.1/jskataUndo.js">v.01</a> of the undo and I now handle undo and redo in the same object.</p>
<h3>Annoucing redo in jskata.undo</h3>
<p>I made a new version of <em>jskata.undo</em> still <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.undo.js">hosted on GitHub</a>. If you look closer, it is now part of a library called <a href="http://github.com/dsimard/jskata">jskata</a> that is not officially announced. My next post will talk about it in details.</p>
<h3>Demo</h3>
<p>Take a look at the <a href="http://dsimard.github.com/jskata/undo" target="_blank">demo</a>. The javascript of the demo is available <a href="http://github.com/dsimard/jskata/blob/gh-pages/undo.js#files" target="_blank">here</a>. You can see the <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.undo.textile#files">complete doc on GitHub</a>.</p>
<h3>How does it work?</h3>
<h4>Execute an action that you can undo/redo</h4>
<p>Doing something requires calling <em>execute</em> with 2 functions as parameters : the <em>do</em> and the <em>undo</em>.<br />
<script src="http://gist.github.com/379819.js"></script></p>
<h4>Undo and redo the last action</h4>
<p>Very easy!<br />
<script src="http://gist.github.com/379830.js?file=call+undo+and+redo.js"></script></p>
<h4>Events and properties</h4>
<p>For the moment, jskata.undo has just one event : <em>onChange</em>.<br />
<script src="http://gist.github.com/379834.js?file=jskata.undo.onchange.js"></script></p>
<p>There are 2 properties : <em>canUndo()</em> and <em>canRedo()</em>.<br />
<script src="http://gist.github.com/379840.js?file=canUndo+and+canRedo.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/feed/</wfw:commentRss>
		<slash:comments>7</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/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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>dsimard</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>10</slash:comments>
		</item>
		<item>
		<title>How to write a singleton class in javascript</title>
		<link>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-write-a-singleton-class-in-javascript</link>
		<comments>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 12:59:54 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=166</guid>
		<description><![CDATA[If I look at my stats, a lot of people are wondering how to write a singleton class. I already <a href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/" target="_blank">wrote about it before</a> but my old solution exposed the instance of the class so more than one instance could be created thus making it not completly a singleton class.]]></description>
			<content:encoded><![CDATA[<div style="color:red;font-weight:bold;margin-bottom:1em;">[UPDATE : This post is outdated. Check out the <a href="http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/">new post on singletons</a>.]</div>
<p>If I look at my stats, a lot of people are wondering how to write a singleton class. I already <a href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/" target="_blank">wrote about it before</a> but my old solution exposed the instance of the class so more than one instance could be created thus making it not completly a singleton class.</p>
<p>Here&#8217;s the new solution using a <a href="http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/">private variable</a> for the instance.</p>
<p><script src="http://gist.github.com/1163955.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to create an object with private variables and methods</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-create-an-object-with-private-variables-and-methods</link>
		<comments>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 12:50:52 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128</guid>
		<description><![CDATA[In short, you can use private variables when you return another scope when declaring a class.]]></description>
			<content:encoded><![CDATA[<p>In short, you can use private variables when you return another scope when declaring a class.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Cats<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="kw2">var</span> nameList = <span class="br0">&#91;</span><span class="br0">&#93;</span>; <span class="co1">// private var</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is where you define another scope!</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; add:<span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; nameList.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How does it work?</h3>
<p>The magic lies in creating a different scope at the end of the class definition that does not include private variables. Then, private members are <strong>available in this scope and not outside of it</strong>, thanks to the <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/" target="_blank">power of closures</a>.</p>
<h3>Differences between private and public</h3>
<p>These two classes definition shows the difference between the a class where all members are public versus a class where some members are private.</p>
<p>This is a class where <strong>all members are public</strong>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> PublicCats<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">// This is the list of cat names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">nameList</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// This is a method that I would like to be private but can&#8217;t</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// It returns the last cat of the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">lastCat</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; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">nameList</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">nameList</span>.<span class="me1">length</span><span class="nu0">-1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Return the list of names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">names</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; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">nameList</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add a name to the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">add</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">nameList</span>.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// Return the last cat just added</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">lastCat</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This is the corresponding class where <strong>some members are private</strong>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> PrivateCats<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">// This is the list of cat names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> nameList = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// This is a private method</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> lastCat = <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; &nbsp; <span class="co1">// Note : I don&#8217;t use &quot;this&quot; to access private variables</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// thanks to the power of closures!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> nameList<span class="br0">&#91;</span>nameList.<span class="me1">length</span><span class="nu0">-1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// These are our public methods!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is where we create another scope to</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// avoid external objects to use the private variables.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; add:<span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Note : once again, I don&#8217;t use &quot;this&quot; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// to access the private variables and methods</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; nameList.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> lastCat<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; names:<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; &nbsp; &nbsp; <span class="kw1">return</span> nameList;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In the above code, line 15 makes all the difference between the two classes.</p>
<h3>On GitHub</h3>
<p>Get all the <a href="http://github.com/dsimard/jskata_examples/blob/master/private/private.js">code on GitHub</a> and see it live in action on <a href="http://dsimard.github.com/jskata_examples/private.html">my GitHub page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>How to use JSON (updated with example)</title>
		<link>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-use-json-updated-with-example</link>
		<comments>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 15:14:48 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[libraries]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=91</guid>
		<description><![CDATA[This post is an update from the <a href="/2007/05/08/how-to-use-json/" target="_self">old post</a>. A lot of things changed since it was written and the information in the old one is a bit outdated.]]></description>
			<content:encoded><![CDATA[<p>This post is an update from the <a href="http://www.javascriptkata.com/2007/05/08/how-to-use-json/" target="_self">old post</a>. A lot of things changed since it was written and the information in the old one is a bit outdated.</p>
<p>First of all, <strong>you should never use an <em>eval()</em> when using JSON</strong> because of security reasons that I will talk about later in another post. [edit : jQuery uses <em>eval()</em> but there's a work-around that will be talked about in another post.]</p>
<h3>JSON and jQuery</h3>
<p>The simplest way to use JSON is though <a href="http://jquery.com/" target="_blank">jQuery</a>. I have been using for <a href="http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/">more than 2 years</a> and I never was disappointed.</p>
<p>Let&#8217;s say I want to call <a href="http://www.flickr.com/services/api/flickr.photos.search.html" target="_blank">Flickr search</a> with JSON. It&#8217;s simple :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">getJSON</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;http://api.flickr.com/services/rest/?jsoncallback=?&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> method : <span class="st0">&quot;flickr.photos.search&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; api_key : <span class="st0">&quot;4ef2fe2affcdd6e13218f5ddd0e2500d&quot;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; format : <span class="st0">&quot;json&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags : <span class="st0">&quot;landscape&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; ajaxCallBack</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>The <em>$.getJSON</em> takes 3 parameters.</p>
<p><strong>1. The URL</strong><br />
The URL of the remote call.</p>
<p><strong>2. The parameters</strong><br />
The parameters to complete the call. On the <a href="http://www.flickr.com/services/api/flickr.photos.search.html" target="_blank">documentation page</a> about this Flickr method, you have a list of all the parameters you can use. For a JSON call on Flickr, you have to send the <strong>api_key</strong> and the <strong>format</strong>. I added the parameter <strong>tags</strong> to send me all photos that are tagged <em>landscape</em>.</p>
<p><strong>3. The callback</strong><br />
Most API services now requires a callback, a method that will be called when the JSON is loaded. It is more secure and easier to work with. At the end of the first parameter (the URL), I added <em>jsoncallback=?</em> (flickr called this parameter <em>jsoncallback</em> but it could be named differently on other services). This is the jQuery-way of saying &#8220;When you finish loading the JSON, call the method specificied by the third argument&#8221;.</p>
<p>You may wonder how Flickr knows which function to use on callback. It&#8217;s because in jQuery, there&#8217;s a special case for that. In the URL, you can see that the <em>jsoncallback</em> is written like that <strong>jsoncallback=?</strong>. The <strong>?</strong> is replaced by your own callback method (in my case, I called it <em>ajaxCallback</em>)</p>
<h3>The callback method</h3>
<p>The callback method I used looks like this :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> ajaxCallBack<span class="br0">&#40;</span>data<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Loop throught all photos and display them</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// it uses the jquery.each method </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// doc at http://docs.jquery.com/Utilities/jQuery.each</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; $.<span class="me1">each</span><span class="br0">&#40;</span>data.<span class="me1">photos</span>.<span class="me1">photo</span>, <span class="kw2">function</span><span class="br0">&#40;</span>photoIdx, photo<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build the thumbnail url</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> thumb_url = <span class="br0">&#91;</span><span class="st0">&quot;http://farm&quot;</span>, photo.<span class="me1">farm</span>, <span class="st0">&quot;.static.flickr.com/&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.<span class="me1">server</span>, <span class="st0">&quot;/&quot;</span>, photo.<span class="me1">id</span>, <span class="st0">&quot;_&quot;</span>, photo.<span class="me1">secret</span>, <span class="st0">&quot;_t.jpg&quot;</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build the photo url</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> photo_url = <span class="br0">&#91;</span><span class="st0">&quot;http://www.flickr.com/photos/&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.<span class="me1">owner</span>, <span class="st0">&quot;/&quot;</span>, photo.<span class="me1">id</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build HTML</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;body&quot;</span><span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;&lt;a&gt;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;href&quot;</span>, photo_url<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;target&quot;</span>, <span class="st0">&quot;_blank&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">append</span><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;&lt;img&gt;&quot;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;src&quot;</span>, thumb_url<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>If you know jQuery a little, it simply creates the HTML to show the thumbnails and a link to the photo.</p>
<h3>Get the code</h3>
<p>You can get the complete code for this example on <a href="http://github.com/dsimard/jskata_examples/tree/simple" target="_blank">GitHub</a> and a <a href="http://dsimard.github.com/jskata_examples/flickr_json.html">working example</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/feed/</wfw:commentRss>
		<slash:comments>5</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/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[libraries]]></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>8</slash:comments>
		</item>
	</channel>
</rss>

