<?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>DaveOnCode &#187; closure</title>
	<atom:link href="http://www.daveoncode.com/tag/closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Google Closure&#8217;s idiocies: Ajax can&#8217;t be synchronous :(</title>
		<link>http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/</link>
		<comments>http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 11:02:08 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=564</guid>
		<description><![CDATA[While I love Closure tools and the possibility to write a better organized and OOP based JavaScript, I&#8217;m facing some weakness and inexplicable choices in the library. The last discover is that is impossible to make synchronous ajax call using the provided classes, since I didn&#8217;t find nothing about synch/asynch options by reading API reference, [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/' addthis:title='Google Closure&#8217;s idiocies: Ajax can&#8217;t be synchronous :( ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>While I love Closure tools and the possibility to write a better organized and OOP based JavaScript, I&#8217;m facing some weakness and inexplicable choices in the library. The last discover is that is impossible to make synchronous ajax call using the provided classes, since I didn&#8217;t find nothing about synch/asynch options by reading API reference, I took a look to the source (xhrio.js), and I noticed this:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">xhr_</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>method<span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #006600; font-style: italic;">// Always async!</span></div></td></tr></tbody></table></div>
<p>that <strong>true</strong> should be a parametric value which developers can set to true or false. Why forcing it to true???<br />
I&#8217;m really disappointed :(</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/' addthis:title='Google Closure&#8217;s idiocies: Ajax can&#8217;t be synchronous :( ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Custom JavaScript classes and packages using goog.provide() and goog.require()</title>
		<link>http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/</link>
		<comments>http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 15:38:39 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=538</guid>
		<description><![CDATA[I will repeat it until the dead, the real power of Goolge Closure is the mechanism and the tools behind it, not the classes and methods written for you, but the possibility to write your own better JavaScript code! Today I&#8217;m gonna show you how to create and use your custom JavaScript classes and to [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/' addthis:title='Custom JavaScript classes and packages using goog.provide() and goog.require() ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I will repeat it until the dead, the real power of Goolge Closure is the mechanism and the tools behind it, not the classes and methods written for you, but the possibility to write your own better JavaScript code! Today I&#8217;m gonna show you how to create and use your custom JavaScript classes and to take the benefits of <strong>goog.provide()</strong> and <strong>goog.require()</strong> methods.<br />
Ok, let&#8217;s suppose we are working on a funny and exciting project, like an online  JavaScript game called &#8220;Js Monsters Battle&#8221; and let&#8217;s suppose the domain will be www.jsmonstersbattle.com. We love OOP, we love write js code, we love to have a well organized and document code, so we will begin by creating our packages, following the standard which establishes that the root of a package must be the reversed domain name, so in our example we will create this folders structure &#8220;/com/jsmonstersbattle/&#8221; and inside we will put our classes (and subpackages).<br />
Since our game is a game about monsters, we will create the core package &#8220;monsters&#8221; in that directory and we will write the first base class called Monster. We will got the following: &#8220;/com/jsmonstersbattle/monsters/monster.js&#8221;, three folders and one js file. Yes, one JavaScript file for class, forget those huge files containing millions of functions, we are going to write better JavaScript code and thinking more like a Java developer. If you are concerning about file size and performances, don&#8217;t worry about it, the compiler will reduce and optimize the code for you (just before release the project). Ok, let&#8217;s take a look inside the Monster class (monster.js):</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.Monster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* A common Monster<br />
&nbsp;* @constructor<br />
&nbsp;* @param {String} name<br />
&nbsp;* @param {String} age<br />
&nbsp;* @param {Number} level<br />
&nbsp;*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._name <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._age <span style="color: #339933;">=</span> age<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._level <span style="color: #339933;">=</span> level<span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
* Returns an amount of damage points<br />
* @return {Number}<br />
*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">attack</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._level <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>._age <span style="color: #339933;">/</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In the code above I defined a basic class called <strong>Monster</strong>, which is available under the package (namespace) <strong>com.jsmonstersbattle.monsters</strong>, declared using <strong>goog.provide()</strong>, then I created a constructor (by using the special jsDoc notation /** @constructor */) which receives three arguments and sets three private Monster&#8217;s variable (/** @private */), finally I defined a method <strong>attack</strong> which returns a number of damage points. Ok, by following the Monster&#8217;s example, we can now create as many classes and packages as we like, interfaces (using /** @interface */ and /** @impements */) and subcalsses (using the method <strong>goog.inherits()</strong>), let&#8217;s see briefly these implementation:</p>
<p><strong>Extending a class:</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
* @constructor<br />
* @extends com.jsmonstersbattle.monsters.Monster<br />
*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">UglyMonster</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #339933;">,</span> uglyness<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// call to Super</span><br />
&nbsp; &nbsp; com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._uglyness <span style="color: #339933;">=</span> uglyness<span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// you can read the following as: &quot;UglyMonster extends Monster&quot;</span><br />
goog.<span style="color: #660066;">inherits</span><span style="color: #009900;">&#40;</span>com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">UglyMonster</span><span style="color: #339933;">,</span> com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>Creating and implementing an interface:</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* Evil interface.<br />
&nbsp;* @interface<br />
&nbsp;*/</span><br />
<span style="color: #003366; font-weight: bold;">function</span> IEvil<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
IEvil.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">doEvil</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
* @constructor<br />
* @implements {IEvil}<br />
*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">EvilMonster</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// some code here</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">EvilMonster</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">doEvil</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// IEvil implementation</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Once we created all the necessary classes, we will put our &#8220;com&#8221; folder under &#8220;closure&#8221; folder. Then we will create a main js file which will includes all the code required to initialize and run the application, I called it &#8220;<strong>application.js</strong>&#8221; for convenience. This file will requires all the classes it needs by using <strong>goog.require()</strong>:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.EvilMonster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// ...and so on!</span></div></td></tr></tbody></table></div>
<p>After the import we will be able to use the classes, by referring them with the full qualified class name (&#8220;com.jsmonstersbattle.monsters.EvilMonster&#8221;), however since this means to write long strings, we can create references as the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.EvilMonster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> EvilMonster <span style="color: #339933;">=</span> com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">EvilMonster</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// then...</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> monster <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> EvilMonster<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>or use the JavaScript <strong>with</strong> operator:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.EvilMonster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">with</span> <span style="color: #009900;">&#40;</span>com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> monster <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> EvilMonster<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>&#8230;and this is all today, I hope you enjoyed my post ;^)</p>
<p><strong>UPDATE:<br />
Don&#8217;t use the with operator nor take reference to fully qualified class name, otherwise you will get several errors if you use advanced compilation!</strong></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/' addthis:title='Custom JavaScript classes and packages using goog.provide() and goog.require() ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automatically compile JavaScript applications using Google Closure and Ant</title>
		<link>http://www.daveoncode.com/2009/11/23/automatically-compile-javascript-applications-using-google-closure-and-ant/</link>
		<comments>http://www.daveoncode.com/2009/11/23/automatically-compile-javascript-applications-using-google-closure-and-ant/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 22:06:00 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=527</guid>
		<description><![CDATA[As I said on insideRIA, the power of Google Closure is represented by the additional tools provided by Google: the Java compiler (which compress and optimize your javascript files) and the python script (which calculates dependencies). This tools however are not so user friendly, because you have to rely on the terminal and invoke them [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/11/23/automatically-compile-javascript-applications-using-google-closure-and-ant/' addthis:title='Automatically compile JavaScript applications using Google Closure and Ant ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>As I said on <a href="http://www.insideria.com/2009/11/google-closure-a-new-way-of-de.html">insideRIA</a>, the power of Google Closure is represented by the additional tools provided by Google: the Java compiler (which compress and optimize your javascript files) and the python script (which calculates dependencies). This tools however are not so user friendly, because you have to rely on the terminal and invoke them through command lines, a pretty annoying procedure that can scare people which usually rely only on visual tools.<br />
Fortunately is relatively easy to automatize this compilation, by using Ant (a Java-based build tool). Ant is included by default in Eclipse standard platform (anyway some stand alone Eclipse-based ide such Aptana don&#8217;t include it&#8230; it&#8217;s time to use such programs as Eclipse plugins!) and can be configured and used by simply writing an xml file formally named &#8220;<strong>build.xml</strong>&#8220;.  A build file contains one root node &#8220;<strong>project</strong>&#8221; and at least one &#8220;<strong>target</strong>&#8221; node containing tasks to execute, tasks are a series of different operations that can be executed by Ant, such create/delete files and folders, zip/unzip a file, running scripts and more. To automatize the deployment of our JavaScript application, we will invoke the calcdeps.py and the compiler.jar from Ant in a single statement, and we will print the result to a specific file.<br />
To invoke a script, we have to use the <strong>exec</strong> tag and specify at least the param &#8220;<strong>executable</strong>&#8220;, which indicates the location of the script to execute, then by setting the parameter &#8220;<strong>output</strong>&#8221; the result returned from the script will be redirect to the specified file. Finally, to avoid undesired output in the file, such debug information and errors, we have to specify also a file which will receive this type of data by using the &#8220;<strong>error</strong>&#8221; parameter.<br />
Since, paths to scripts and other files can be very long, we can create custom variables to hold these information and then reusing them in our script invocations. This is as simple as use the &#8220;<strong>property</strong>&#8221; tag and specify a &#8220;<strong>name</strong>&#8221; and a &#8220;<strong>value</strong>&#8221; attributes.<br />
The following is an example of how a build.xml will looks like:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
&nbsp;<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Google Closure Ant Test&quot;</span> </span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">basedir</span>=<span style="color: #ff0000;">&quot;.&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Full path of the current project --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;projectPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/Users/davidezanotti/Documents/workspace/GoogleClosure&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Full path to Closure library --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;closurePath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${projectPath}/closure&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- List of javascript files to compile --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;filesToCompile&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${projectPath}/my-closure-app.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Full path to the compiler jar --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;compilerPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/Users/davidezanotti/Documents/closure-compiler/compiler.jar&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Full path to the compiled file (created if not defined) --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;outputPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${projectPath}/ant-generation.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Full path to the file which will contains debug output and errors --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;logPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${projectPath}/closure-compiler.log&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Compilation level --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;compilation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WHITESPACE_ONLY&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;JavaScript Compilation&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exec</span> <span style="color: #000066;">error</span>=<span style="color: #ff0000;">&quot;${logPath}&quot;</span> <span style="color: #000066;">output</span>=<span style="color: #ff0000;">&quot;${outputPath}&quot;</span> <span style="color: #000066;">executable</span>=<span style="color: #ff0000;">&quot;${closurePath}/bin/calcdeps.py&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;-i ${filesToCompile} -p ${closurePath} -o compiled -c ${compilerPath} -f '--compilation_level=${compilation}'&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exec<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>The build.xml above will first gathering all the necessary Closure files (dependencies) and then will compile the resulting js using a WHITESPACE_ONLY compilation. The result will be printed to the file &#8220;ant-generation.js&#8221; and the debug to &#8220;closure-compiler.log&#8221;.<br />
To run the Ant process: right click on the build.xml and choose Run as Ant Build. This procedure can be also configured in order to execute all the task each time a file in the project is saved, but it&#8217;s not a great idea because the process can takes several seconds to complete.<br />
If you want to learn more about Ant (I have to learn a lot too, since I never used it before :P), check the reference here: <a href="http://ant.apache.org/">http://ant.apache.org</a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/11/23/automatically-compile-javascript-applications-using-google-closure-and-ant/' addthis:title='Automatically compile JavaScript applications using Google Closure and Ant ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/11/23/automatically-compile-javascript-applications-using-google-closure-and-ant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>goog.ui.TabPane: Create tabs with Google Closure</title>
		<link>http://www.daveoncode.com/2009/11/16/googe-closure-create-tabs-component-goog-ui-tabpane/</link>
		<comments>http://www.daveoncode.com/2009/11/16/googe-closure-create-tabs-component-goog-ui-tabpane/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 23:08:36 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=512</guid>
		<description><![CDATA[Hi everyone, by starting with this post I would like to begin a series of posts dedicated to the new javascript library released by Google: Closure! Today I will focus my attention on tabs creation, since this is maybe the most common user interface component in a web application. In order to create Closure tabs, [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/11/16/googe-closure-create-tabs-component-goog-ui-tabpane/' addthis:title='goog.ui.TabPane: Create tabs with Google Closure ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Hi everyone,<br />
by starting with this post I would like to begin a series of posts dedicated to the new javascript library released by Google: Closure!<br />
Today I will focus my attention on tabs creation, since this is maybe the most common user interface component in a web application.<br />
In order to create Closure tabs, we need to import the base js file (/goog/base.js) and then require the class <strong>goog.ui.TabPane</strong>. Inside our head (or body) we will get the following:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;closure/goog/base.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; goog.require('goog.ui.TabPane.TabPage');<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></div></td></tr></tbody></table></div>
<p><span id="more-512"></span></p>
<p>The code above, will do the following: 1. import the base library into the page, 2. dynamically load a series of additional js files which will be used to create and handle tabs components and events. Theoretically we could write more goog.require() statements, but they will be unnecessary because goog.ui.TabPane.TabPage, is able to require all files it needs, that is all the classes it uses internally (such goog.events, goog.dom and so on).<br />
Ok, now that we have all the necessary files, let&#8217;s see how to do. There are fundamentally three ways to create ui components with Closure, one is totally code based, we can create components by writing all things in javascript, we can instead create a semantic HTML structure and then &#8220;convert&#8221; it to a ui component and finally we can mix things and create both components from HTML elements than from the scratch via javascript (in my example I will show this). To use an existent markup as the base for tabs component, we need a container (typically a div) containing a series of subelement (div works better here too) which contain respectively 2 or more subelements among which the first will represents the tab label and the others tab&#8217;s content. In practice the logic structure will be something like:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[tabs-container]<br />
&nbsp; &nbsp; [tab-container]<br />
&nbsp; &nbsp; &nbsp; &nbsp; [tab-label] TAB ONE [/tab-label]<br />
&nbsp; &nbsp; &nbsp; &nbsp; [tab-content] CONTENT ONE [/tab-content]<br />
&nbsp; &nbsp; [/tab-container]<br />
&nbsp; &nbsp; [tab-container]<br />
&nbsp; &nbsp; &nbsp; &nbsp; [tab-label] TAB TWO [/tab-label]<br />
&nbsp; &nbsp; &nbsp; &nbsp; [tab-content] CONTENT TWO [/tab-content]<br />
&nbsp; &nbsp; [/tab-container]<br />
&nbsp; &nbsp; [tab-container]<br />
&nbsp; &nbsp; &nbsp; &nbsp; [tab-label] TAB THREE [/tab-label]<br />
&nbsp; &nbsp; &nbsp; &nbsp; [tab-content] CONTENT THREE [/tab-content]<br />
&nbsp; &nbsp; [/tab-container]<br />
[/tabs-container]</div></td></tr></tbody></table></div>
<p>&#8230;and the HTML will be:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tabs&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>tab 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>tab 1 content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>tab 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>tab 2 content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>tab 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>tab 3 content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></td></tr></tbody></table></div>
<p>As you can see, the type of the tag doesn&#8217;t matter, what matters is the logic structure created.<br />
Now, is possible to create a TabPane components by writing a single line of javascript:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">ui</span>.<span style="color: #660066;">TabPane</span><span style="color: #009900;">&#40;</span>goog.<span style="color: #660066;">dom</span>.$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>With the code above we create a new instance of a TabPane component, which will be rendered using the &#8220;rules&#8221; of the given dom element (<strong>goog.dom.$()</strong> is the same as <strong>document.getElementById()</strong>)<br />
Is really so simple? Yes, but honestly is not so useful to haven&#8217;t a reference to the component, because we can&#8217;t handle events nor add extra elements, moreover our tabs will be display as an ugly pointed list rather than a cool tabs series. In fact Closure doesn&#8217;t apply a style to the ui components, but it simply handle the toggle of tabs by setting the containers with display &#8220;none&#8221; where necessary and making tabs dispatching certain events. Hey&#8230; this sucks! Wait! Closure, adds a series of CSS classes to the HTML nodes and the library provides several CSS (disseminated among the thousands of files) to give components a style (although the default theme is pretty ugly and you will feel the need to write your own stylesheet).<br />
Ok, let&#8217;s see some concrete features and some more code, in order to: add a new tab and its relative content, get specific tab, enable/disable a tab, listen to tab selection/change.<br />
First of all, let&#8217;s take a reference to the TabPane object:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> tabsPanel <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">ui</span>.<span style="color: #660066;">TabPane</span><span style="color: #009900;">&#40;</span>goog.<span style="color: #660066;">dom</span>.$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tabs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Then, we can use its methods and we are able to select an arbitrary tab (the default behavior is to show the first)&#8230; let&#8217;s select the second:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">tabsPanel.<span style="color: #660066;">setSelectedPage</span><span style="color: #009900;">&#40;</span>tabsPanel.<span style="color: #660066;">getPage</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Closure consider a page (an object of type <strong>ui.TabPane.TabPage</strong>) the content related to a tab. The method <strong>setSelectedPage()</strong> takes as first argument an object of that type and we pass it by using <strong>getPage()</strong>, which instead returns the TabPage for a given index.<br />
To add a new page (aka a new tab and its related content) we will use the method addPage in the following way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// create a new dom element</span><br />
<span style="color: #003366; font-weight: bold;">var</span> newPageContainer <span style="color: #339933;">=</span> goog.<span style="color: #660066;">dom</span>.$dom<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// add some text</span><br />
newPageContainer.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;I'm a new page!&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// put the page into TabPane</span><br />
tabsPanel.<span style="color: #660066;">addPage</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> goog.<span style="color: #660066;">ui</span>.<span style="color: #660066;">TabPane</span>.<span style="color: #660066;">TabPage</span><span style="color: #009900;">&#40;</span>newPageContainer<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;new tab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The code is pretty clear, <strong>goog.ui.TabPane.TabPage</strong> takes a dom node and a label as the first arguments.<br />
Ok, let&#8217;s say we would like to disable the previous added page&#8230;  it&#8217;s easy:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">tabsPanel.<span style="color: #660066;">getPage</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We can use its index to retrieve it and then call <strong>setEnabled()</strong>&#8230; of course if we create a reference to the page we can avoid <strong>getPage()</strong> method and enable the tab directly.<br />
Let&#8217;s play a little with event handling, in order to toggle the status of a page (enabled/disabled). We can add a new HTML node:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toggle-btn&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cursor:pointer&quot;</span>&gt;</span>toggle last tab!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span></div></td></tr></tbody></table></div>
<p>and then:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">events</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>goog.<span style="color: #660066;">dom</span>.$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;toggle-btn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// get a reference to the page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> tabsPanel.<span style="color: #660066;">getPage</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// toggle its state (set the opposite of its state ...enabled? -&gt; disabled! ... disabled? -&gt; enabled!)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; p.<span style="color: #660066;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>p.<span style="color: #660066;">isEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// print a message to the console (this will throw an error if not available) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;last tab is now: &quot;</span><span style="color: #339933;">,</span> p.<span style="color: #660066;">isEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;enabled&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;disabled&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>goog.events.listen</strong> takes three fundamental arguments: the dom node to observe, the event name and a callback function.</p>
<p>&#8230;and this is the end! (at the moment)&#8230; if you like this post, add my blog to your bookmarks, because I just begin to dig and write about Google Closure! ;^)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/11/16/googe-closure-create-tabs-component-goog-ui-tabpane/' addthis:title='goog.ui.TabPane: Create tabs with Google Closure ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/11/16/googe-closure-create-tabs-component-goog-ui-tabpane/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

