<?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; librairies</title>
	<atom:link href="http://www.javascriptkata.com/category/librairies/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>An undo/redo library for your app</title>
		<link>http://www.javascriptkata.com/2010/04/26/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>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[jskata]]></category>
		<category><![CDATA[librairies]]></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>4</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/</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>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[librairies]]></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="/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>4</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>
