<?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>Caffeinated Simpleton&#187; Development</title>
	<atom:link href="http://justin.harmonize.fm/index.php/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://justin.harmonize.fm</link>
	<description>A cup of coffee and a soapbox is like a bottle of Jack and a gun.</description>
	<lastBuildDate>Sun, 19 Jun 2011 22:56:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Type Conversion Pedanticism in JavaScript</title>
		<link>http://justin.harmonize.fm/index.php/2010/11/type-conversion-pedanticism-in-javascript/</link>
		<comments>http://justin.harmonize.fm/index.php/2010/11/type-conversion-pedanticism-in-javascript/#comments</comments>
		<pubDate>Tue, 30 Nov 2010 10:23:37 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=371</guid>
		<description><![CDATA[I got in a brief conversation in the node.js IRC channel today about how weird type conversion is in JavaScript. Specifically, this code was suspect: What this shows is that when a Boolean object is used in an if statement, it always evaluates to true, but when it&#8217;s used in a type coercing operator (such [...]]]></description>
			<content:encoded><![CDATA[<p>I got in a brief conversation in the <a href="http://nodejs.org/">node.js</a> IRC channel today about how weird type conversion is in JavaScript. Specifically, this code was suspect:</p>
<p><script src="https://gist.github.com/721409.js"> </script></p>
<p>What this shows is that when a Boolean object is used in an <code>if</code> statement, it always evaluates to <code>true</code>, but when it&#8217;s used in a type coercing operator (such as <code>==</code>), it converts to the value it represents. Why?</p>
<p>The reason is actually pretty straightforward, even though it&#8217;s not obvious when looking at it. <code>if</code> statements convert the result of their expression to a boolean primitive, whereas <code>==</code> converts the right side expression into the type of the left side and checks for equivalency. So <code>if</code> statements don&#8217;t do type conversion at all until the expression is evaluated, and then it implicitly converts the result of that expression into a boolean primitive.</p>
<p>JavaScript is really just doing this:</p>
<p><script src="https://gist.github.com/721428.js?file=how-if-works.js"></script></p>
<p>Where the weirdness comes in is how type converting an object works. If you <a href="http://interglacial.com/javascript_spec/a-9.html">look at the JavaScript spec</a>, you can see that converting an Object to a boolean is always <code>true</code>. And guess what? Instances of <code>Boolean</code> are objects:</p>
<p><script src="https://gist.github.com/721439.js?file=boolean-objects.js"></script></p>
<p>Since new <code>Boolean</code> instances are objects, they will always be <code>true</code> when converted to a boolean primitive, which is exactly what the <code>if</code> statement is doing.<br />
<script src="https://gist.github.com/721473.js?file=converting-boolean-instances.js"></script></p>
<p>So then, why does <code>new Boolean(false) == false</code> result in <code>true</code>? Well it&#8217;s because the rules are different for <code>==</code>. Let&#8217;s say you&#8217;re doing <code>false == new Boolean(false)</code>. This will result in <code>true</code> because the left side of the expression is a primitive, so the interpreter will convert the Boolean instance, not to a boolean, but to a primitive. When converting an object to a value, the interpreter will:</p>
<blockquote><p>Return a default value for the Object. The default value of an object is retrieved by calling the internal [[DefaultValue]] method of the object, passing the optional hint PreferredType. The behaviour of the [[DefaultValue]] method is defined by this specification for all native ECMAScript objects (8.6.2.6).</p></blockquote>
<p>And, you guessed it, the default value of a Boolean instance is just the boolean primitive that it represents. So, after conversion, we&#8217;re testing <code>false</code> against <code>false</code> which is of course true.</p>
<p>The other direction also works. If we&#8217;re doing <code>(new Boolean(false)) == false </code>, then the interpreter will convert <code>false</code> to an <code> Object</code>. The spec specifies that to convert a boolean primitive to an object, it should:</p>
<blockquote><p>Create a new Boolean object whose [[value]] property is set to the value of the boolean.</p></blockquote>
<p>So then we have two equivalent Boolean instances being compared, which again results in <code>true</code></p>
<p><script src="https://gist.github.com/721480.js?file=boolean-type-summary.js"></script></p>
<p>Pretty crazy, but it kind of makes sense when you think about it. Maybe.</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2010/11/type-conversion-pedanticism-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bujagali: Incredibly Fast JavaScript Templating</title>
		<link>http://justin.harmonize.fm/index.php/2010/10/bujagali-incredibly-fast-javascript-templating/</link>
		<comments>http://justin.harmonize.fm/index.php/2010/10/bujagali-incredibly-fast-javascript-templating/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 07:31:08 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Bujagali]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=354</guid>
		<description><![CDATA[&#8220;Fuck it, let&#8217;s do it live&#8221; Today&#8217;s complex web apps have a complicated mish-mash of server-side templates combined with client-side JavaScript. Often times there are certain calls to the server that return HTML, and others that return JSON or XML for client-side use. This not only distorts the &#8220;view&#8221; portion of MVC, but it can [...]]]></description>
			<content:encoded><![CDATA[<h3>&#8220;Fuck it, let&#8217;s do it live&#8221;</h3>
<p>Today&#8217;s complex web apps have a complicated mish-mash of server-side templates combined with client-side JavaScript. Often times there are certain calls to the server that return HTML, and others that return JSON or XML for client-side use. This not only distorts the &#8220;view&#8221; portion of MVC, but it can also be hard to develop for and, increasingly, slow. At <a title="Rdio" href="http://rdio.com" target="_blank">rdio</a>, we thought that we may be able to see substantial performance improvements across the board if we changed our templating engine from <a href="http://jinja.pocoo.org/" target="_blank">Jinja2</a> on the server-side to a custom, client-side templating engine. We found this to be quite true in the end, and the majority of the pages found on <a href="http://rdio.com">rdio</a> today use <a href="http://github.com/rdio/bujagali" target="_blank">Bujagali</a> (the name we gave our system).</p>
<h3>Goals</h3>
<p>The goals for the project were pretty ambitious.</p>
<ul>
<li>Trivial to convert from Jinja to Bujagali
<ul>
<li>Support template inheritance</li>
<li>Support importing shared code</li>
<li>Support filters (essentially just functions accessible from the template)</li>
</ul>
</li>
<li>Make heavy use of client&#8217;s cache</li>
<li>Reduce bandwidth usage to being as close as possible to the size of the actual data required to render</li>
<li>Perform really, really fast on good browsers</li>
<li>Not have terrible performance in IE</li>
<li>Optionally render server-side to support googlebot and other limited JS environments</li>
</ul>
<p>We achieved these goals, and you can learn how to use the engine and play around with the source <a href="http://github.com/rdio/bujagali/">on github</a>. Be warned, however, that documentation is sparse right now.</p>
<p>If you want to learn how all the insides work, keep reading.</p>
<h3>Architecture</h3>
<p>The architecture is pretty simple on the surface. It comes down to heavy caching, robust versioning, a laissez-faire syntax, and flexibility in runtime environment.</p>
<p>A quick note about measuring performance: it&#8217;s hard. Bujagali is optimized to perform better over time, to get you your data faster by utilizing smaller payloads, and to actually be fast once we were shoving data into strings. To top it off, it performs differently depending on your client. It&#8217;s hard to do an apples-to-apples comparison of overall template performance in a case like this, but suffice it to say that we measured performance extensively at <a href="http://rdio.com">rdio</a> and Bujagali is way, way faster in most cases than the previous Jinja2-based system. I hope to write more about performance in the future.</p>
<h5>Caching</h5>
<p>When it comes to caching, there&#8217;s one overriding rule: templates themselves are very cacheable, data is not. I also did not assume that the application JavaScript was very cacheable since in rdio&#8217;s case it&#8217;s not. After all, we roll out new versions of the site every day and since the application JavaScript is compressed into one file, your browser almost always has to reload the JavaScript whenever we roll out a new version. I wanted to avoid this in the case of templates for a couple of reasons. First of all, most of the templates change quite a bit less frequently than the JavaScript itself, and second, including all markup on the site would increase the size of the rapidly-changing JS file enormously.</p>
<p>To maximize cacheability of the templates, we turn to the browser cache itself. Each template is just a JavaScript file that contains a JavaScript function of the same name. By including the version of the file in the file name and telling the browser to cache it til we&#8217;re all long dead, we can assure that the turnaround time for the browser to get the template loaded into the program is really low. In fact in some versions of IE, loading a script that is contained in the cache is a synchronous operation. This is better explained through a bit of code.</p>
<p><script src="http://gist.github.com/633720.js?file=load.js"></script> As long as we carefully keep track of versions, we can be sure that the function loaded by the above script is the one that we want and that if the template does not change, it will never require another trip to the server to retrieve. In practice, this is a huge win as the full markup of your site really only has to be transferred once.</p>
<h5>Versioning</h5>
<p>The next overriding principle was strict versioning. This was pretty much necessary as a result of the heavy caching. Every time data is passed into Bujagali to be rendered, it must specify what version of the template it expects to be rendered against. By providing this information every time, we can be sure that the templates always work right in pretty much every possible proxy and caching set-up.  A cool side effect of this is hot-swappable templates. If you modify a template server side and an ajax request comes in for data that&#8217;s supposed to render against that freshly modified template, Bujagali will reload the template and render it seamlessly, no refresh required.</p>
<h5>Laissez-Faire Syntax</h5>
<p>The final part of designing Bujagali was figuring out what we would actually allow in the templates. Jinja is very strict about what you can and cannot do. Bujagali is not. You can just write arbitrary JavaScript, and in fact writing a template in Bujagali feels a lot more like writing JS than it does HTML. This isn&#8217;t because I think there should be logic in your templates (I don&#8217;t), but I think that it&#8217;s up to the developer to decide what constitutes &#8220;program logic&#8221; and what constitutes &#8220;view logic&#8221;. Having a full-fledged language can be very useful in a template, and so arbitrary JavaScript is perfectly acceptable in Bujagali. It&#8217;s a language you already know and it won&#8217;t get in your way.  An example comes from one of rdio&#8217;s own templates:<br />
 <script src="http://gist.github.com/633752.js?file=nav_bar.js"></script></p>
<h5>Flexibility in Runtime Environment</h5>
<p>Despite the fact that Bujagali allows you to execute arbitrary javascript, it does not make a lot of guarantees about the environment. Bujagali requires the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a> library, so it will always be available to templates, but beyond that it makes no guarantees (although it is extensible, so you can ensure that your own objects and helpers are available). This restriction gives us the flexibility to render templates either on the client side or on the server side using a server-side JavaScript environment like <a href="http://nodejs.org/">node.js</a>.</p>
<p>This restriction on environment does have some practical benefits that keep the templates &#8220;clean&#8221;, despite the arbitrary JS that is permitted. Without access to the DOM or the rest of the running program, side effects from the templates themselves are limited. This restriction is not actually enforced by Bujagali itself, however, instead you&#8217;ll just get burned if you don&#8217;t stick to the restriction and then try to use your template in a different context.</p>
<h3>Try it out</h3>
<p>I&#8217;ll write more about actually using Bujagali in the future. However, the code is available now <a href="http://github.com/rdio/bujagali/" target="_blank">on github</a>. The documentation is a bit thrown together right now, but it&#8217;s hopefully enough to get you started. There&#8217;s some infrastructure required since, unlike a normal templating system, this system spans the boundary between client and server. Luckily the code provided should make it fairly easy to get going if you already know what you&#8217;re doing.</p>
<p>It should be stated that this isn&#8217;t exactly a templating system for the faint of heart. Best of luck to all who venture on to github.</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2010/10/bujagali-incredibly-fast-javascript-templating/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Cobra is Ready to Release</title>
		<link>http://justin.harmonize.fm/index.php/2010/02/cobra-is-ready-to-release/</link>
		<comments>http://justin.harmonize.fm/index.php/2010/02/cobra-is-ready-to-release/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 08:06:40 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Cobra]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=346</guid>
		<description><![CDATA[I did a little work on Cobra today and pushed the code around a little bit. Things that changed: Reorganized into a single closure. This helps keep internal details internal. Minimize with Closure Compiler JSLint now passes I definitely recommend the JSLint VIM plugin if you&#8217;re a vimmer. Works in node.js (and most likely any [...]]]></description>
			<content:encoded><![CDATA[<p>I did a little work on Cobra today and pushed the code around a little bit. Things that changed:</p>
<ul>
<li>Reorganized into a single closure. This helps keep internal details internal.</li>
<li>Minimize with <a href="http://code.google.com/closure/compiler/" target="_blank">Closure Compiler</a></li>
<li><a href="http://www.jslint.com/" target="_blank">JSLint</a> now passes
<ul>
<li>I definitely recommend the <a href="http://github.com/hallettj/jslint.vim" target="_blank">JSLint VIM plugin</a> if you&#8217;re a vimmer.</li>
</ul>
</li>
<li>Works in <a href="http://nodejs.org/" target="_blank">node.js</a> (and most likely any server-side JavaScript implementation)</li>
<li>Works in Internet Explorer.
<ul>
<li>I had never really tested this, but after fixing the things that JSLint found, the tests just passed.</li>
<li>Still need to test in IE 6. I don&#8217;t have a machine with IE 6 unfortunately.</li>
</ul>
</li>
<li>Added to documentation to clarify a few things.</li>
</ul>
<p>At this point, I think the library&#8217;s safe for others to use. I tagged a 0.5 version, and I&#8217;ll be calling it 1.0 after it gets into a few more projects. Feel free to try it out!</p>
<p>You can find <a href="http://github.com/jmtulloss/cobra">Cobra on github</a>, or you can <a href="http://github.com/jmtulloss/cobra/zipball/0.5">download version 0.5 now</a> (<a href="http://github.com/jmtulloss/cobra/tarball/0.5">tgz</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2010/02/cobra-is-ready-to-release/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>An Introduction to JavaScript&#8217;s &#8220;this&#8221;</title>
		<link>http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/</link>
		<comments>http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 01:27:43 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[this]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=298</guid>
		<description><![CDATA[JavaScript is an amazing little language, but it&#8217;s got some quirks that turn a lot of people off. One of those quirks is this, and how it&#8217;s not necessarily what you expect it to be. this isn&#8217;t that complicated, but there are very few explanations of how it works on the internet. I find myself [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is an amazing little language, but it&#8217;s got some quirks that turn a lot of people off. One of those quirks is <code>this</code>, and how it&#8217;s not necessarily what you expect it to be. <code>this</code> isn&#8217;t that complicated, but there are very few explanations of how it works on the internet. I find myself constantly re-explaining the concept to those who are new to JavaScript development. This article is an attempt to explain how <code>this</code> works and how to use it properly.</p>
<h2>What is <code>this</code>?!</h2>
<p><code>this</code> is the current object. People that are used to object oriented languages use objects constantly, and <code>this</code> is how you access your object in JavaScript. This differs from Java and C++. This is best demonstrated by example, so let&#8217;s take a look.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> HotDog <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
     string getCondiments<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
     string condiments;
<span style="color: #008000;">&#125;</span>
&nbsp;
string HotDog<span style="color: #008080;">::</span><span style="color: #007788;">getCondiments</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> condiments; <span style="color: #666666;">// The compiler knows to find &quot;condiments&quot; in the current HotDog instance</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Ruby and Python don&#8217;t work this way. Instead, you must say &#8220;look for this variable in the current instance&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> HotDog:
    condiments
    <span style="color: #ff7700;font-weight:bold;">def</span> getCondiments<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
         <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">condiments</span> <span style="color: #808080; font-style: italic;"># &quot;self&quot; is a reference to the current instance of HotDog.</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> HotDog
         <span style="color:#0066ff; font-weight:bold;">@condiments</span>
         <span style="color:#9966CC; font-weight:bold;">def</span> getCondiments
              <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@condiments</span> <span style="color:#008000; font-style:italic;"># @condiments is an instance variable of an instance of HotDog</span>
         <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>JavaScript has more in common with Ruby and Python than with Java and C++. The same thing in JavaScript is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">condiments</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;mustard, ketchup&quot;</span>;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getCondiments</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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">condiments</span>; <span style="color: #006600; font-style: italic;">//this is expected to be a reference to the current instance</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s what <code>this</code> is expected to be, anyway. It&#8217;s expected to be a reference to the current instance of whatever object it&#8217;s defined within.</p>
<h3><code>this</code> Gets Tricky</h3>
<p>However, let&#8217;s say we wanted to find out what condiments were on our hotdog in 30 seconds. Assuming the HotDog class from above, that code might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myHotDog <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Call the getCondiments function in 3 seconds</span>
setTimeout<span style="color: #009900;">&#40;</span>myHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Many JavaScript beginners are surprised to learn that this code won&#8217;t work. It&#8217;ll give an error saying that <code>this</code> doesn&#8217;t have a member called <code>condiments</code>, even though it clearly does. What happened?!</p>
<p>As it turns out, <code>this</code> in the <code>getCondiments</code> function is set to the global object, <code>window</code>. This is because there is no binding of functions to instances in JavaScript. Whenever the instance isn&#8217;t explicitly stated, then <code>this</code> becomes <code>window</code> (at least in the browser). Writing this as <code>myHotDog.getCondiments()</code> indicates that you want <code>this</code> to be <code>myHotDog</code>, so it works correctly. The <code>setTimeout</code> function, however, just has a reference to that function. When it calls it, it&#8217;s not aware of <code>myHotDog</code>, so JavaScript sets <code>this</code> to <code>window</code></p>
<h2>&#8220;Fixing&#8221; <code>this</code></h2>
<p>There are several ways of forcing this to be what you want, and many of them touch on some of the more interesting features available in JavaScript. The first is <code>apply</code>. <code>apply</code> is a member of every function. It says &#8220;call this function with this object as this and these arguments. An example might help.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// These two are equivalent</span>
myHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
myHotDog.<span style="color: #660066;">getCondiments</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>myHotDog<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// You could also do this</span>
<span style="color: #003366; font-weight: bold;">var</span> yourHotDog <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
myHotDog.<span style="color: #660066;">getCondiments</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>yourHotDog<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// The above line does the same thing as:</span>
yourHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// The above two calls will return the condiments off of your hot dog, not mine.</span></pre></div></div>

<p>That doesn&#8217;t solve our <code>setTimeout</code> problem, however. <code>apply</code> actually calls the function, and we just want a reference to the function that uses the correct <code>this</code>. Luckily this is easily done, but let&#8217;s talk about some background first.</p>
<h3>Lexical Scoping</h3>
<p>In many languages (all the good ones, if you ask me), <a href="http://en.wikipedia.org/wiki/Scope_%28programming%29#Static_scoping_.28also_known_as_lexical_scoping.29">lexical scoping</a> is supported. Lexical scoping basically allows you to access local variables in a parent function. If a function is defined within another function, it can access its own local variables as well as those of the function it was defined within. Time for another example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> drinkBeer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> beer <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Big Daddy IPA&quot;</span>;
    <span style="color: #003366; font-weight: bold;">function</span> pour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003366; font-weight: bold;">var</span> glass <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Pint Glass&quot;</span>;
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Poured &quot;</span> <span style="color: #339933;">+</span> beer <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; into a &quot;</span> <span style="color: #339933;">+</span> glass;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #003366; font-weight: bold;">function</span> drink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         beer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span>;
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Beer is all gone&quot;</span>;
   <span style="color: #009900;">&#125;</span>
   pour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
   drink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
drinkBeer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This works just fine. <code>drinkBeer</code> cannot access <code>glass</code>, but <code>pour</code> and <code>drink</code> can access <code>beer</code>.</p>
<p>Lexical scoping is extremely powerful. If this small example doesn&#8217;t explain it, I encourage you to look around other examples on the internet until it&#8217;s more clear. While writing JavaScript, look out for ways this can make your life easier. Once you&#8217;re looking, they&#8217;re all over the place.</p>
<h3>Solving our <code>setTimeout</code> Problem</h3>
<p>With lexical scoping, we can easily solve our <code>setTimeout</code> problem.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myHotDog <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
setTimeout<span style="color: #009900;">&#40;</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>
    myHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>See what we did there? We created a new function that we passed to <code>setTimeout</code>. Our new function can access <code>myHotDog</code>, so it just applies it to the <code>getCondiments</code> function. Pretty slick.</p>
<h3>Defending against <code>this</code> abuse</h3>
<p>As somebody writing the <code>HotDog</code> class, it might be upsetting to you that people constantly need to keep <code>this</code> in mind when accessing your class. It would be nicer if there was a way to make your class work all the time. Fortunately, there is!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>; <span style="color: #006600; font-style: italic;">// self references the current this, which is correct.</span>
    self.<span style="color: #660066;">condiments</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;mustard, ketchup&quot;</span>;
    self.<span style="color: #660066;">getCondiments</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: #000066; font-weight: bold;">return</span> self.<span style="color: #660066;">condiments</span>; <span style="color: #006600; font-style: italic;">// self is guaranteed to be a reference to the original &quot;this&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>HotDog</code> is a constructor. You instantiate a new instance of <code>HotDog</code> by writing &#8220;<code>new HotDog()</code>&#8220;. In constructors, <code>this</code> is always your instance. So we created a new variable, my, that references the <code>HotDog</code> instance. This allows you to always refer to the <code>HotDog</code> instance, no matter how the <code>getCondiments</code> function is called.</p>
<h3>Bind</h3>
<p>Another method of &#8220;fixing&#8221; <code>this</code> is adding a <code>bind</code> attribute to every function. This allows you to pass in an object that will be your <code>this</code>. Many JavaScript libraries, such as <a href="http://www.prototypejs.org/">Prototype</a>, do this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> boundFunction <span style="color: #339933;">=</span> myHotDog.<span style="color: #660066;">getCondiments</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>myHotDog<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now <code>boundFunction</code> will always call <code>getCondiments</code> with <code>this</code> set to <code>myHotDog</code>. If you&#8217;ve been paying attention, it should be fairly obvious how bind is written.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> bind<span style="color: #009900;">&#40;</span>thisObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>; <span style="color: #006600; font-style: italic;">// this is the function here</span>
    <span style="color: #000066; font-weight: bold;">return</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>
        self.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>thisObject<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// (arguments is a keyword that refers to the passed in arguments)</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Cobra</h3>
<p>My own class library, <a href="http://justin.harmonize.fm/index.php/2009/01/cobra-a-little-javascript-class-library/">cobra</a>, solves the <code>this</code> problem in a different way. It passes a reference to the original <code>this</code> to every function, which allows you to use some fancy features like prototypal inheritance while still not worrying about binding and the like. If you&#8217;re interested, you can <a href="https://github.com/JustinTulloss/cobra">find the code on github.</a>.</p>
<h2>Whew</h2>
<p>That&#8217;s pretty much all you need to know about <code>this</code>.</p>
<ul>
<li><code>this</code> will be <code>window</code> whenever your function is indirectly called.</li>
<li>Use <code>apply</code> to force <code>this</code> to be what you want</li>
<li>You can use lexical scoping to make sure <code>this</code> behaves in a predictable manner</li>
</ul>
<p>I hope that&#8217;s clear, comment if it&#8217;s not!</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
		</item>
		<item>
		<title>Meetup.com webOS Client Part 1: Services</title>
		<link>http://justin.harmonize.fm/index.php/2009/07/meetup-com-webos-client-part-1-services/</link>
		<comments>http://justin.harmonize.fm/index.php/2009/07/meetup-com-webos-client-part-1-services/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 04:46:23 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Example-Meetup-Client]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Meetup.com]]></category>
		<category><![CDATA[Mojo]]></category>
		<category><![CDATA[webOS]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=283</guid>
		<description><![CDATA[Palm&#8217;s Mojo SDK has just been released to the public this past week. I thought I would take the opportunity to show off some of the awesome things the SDK can do. The Mojo SDK is exceptionally good at tying your life together with the web services you use every day, so my first series [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://developer.palm.com">Palm&#8217;s Mojo SDK</a> has just been released to the public this past week. I thought I would take the opportunity to show off some of the awesome things the SDK can do. The Mojo SDK is exceptionally good at tying your life together with the web services you use every day, so my first series will be on building a simple Meetup.com client. At first, we&#8217;ll just sync down meetups using the <a href="http://www.meetup.com/meetup_api/key/" target="_blank">api key that&#8217;s associated with every individual account</a>. Another day, we&#8217;ll add OAuth authentication to make it generally useful for anybody.</p>
<p>This article assumes that you have some knowledge of how to create an app, new scenes, and how to debug. If you aren&#8217;t somewhat comfortable with the SDK yet, check out the <a href="http://developer.palm.com/index.php?option=com_content&amp;view=article&amp;id=1758">Hello World tutorial</a>.</p>
<h3>Setup</h3>
<p>At first, we&#8217;ll just create a big button that puts all our meetups into the calendar. Nothing fancy. To create the scene, run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ palm-generate <span style="color: #660033;">-t</span> new_scene <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;name=sync&quot;</span></pre></div></div>

<p>Then in the stage-assistant.js file, put:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">controller</span>.<span style="color: #660066;">pushScene</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sync&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h3>Creating a Calendar</h3>
<p>The first thing that we will do is create a new calender for Meetup.com. This calendar will appear in the calendar app right next to your Google Calendar or Exchange calendar using the Palm Synergy APIs. This is great because it allows you to deliver new data to your users without having to write yet another way of presenting it. All contacts and calendars can be plugged straight into the core webOS applications.</p>
<p>To create a calendar, you first need to <a href="http://developer.palm.com/index.php?option=com_content&#038;view=article&#038;id=1737">make an account</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">accountServiceId</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;palm://com.palm.accounts/crud/&quot;</span>;
&nbsp;
 <span style="color: #009966; font-style: italic;">/* Retrieves account if it exists, otherwise creates it */</span>
setupAccount<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">accountServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'listAccounts'</span><span style="color: #339933;">,</span>
        parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got account list: %j&quot;</span><span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>list.<span style="color: #660066;">list</span> <span style="color: #339933;">&amp;&amp;</span> list.<span style="color: #660066;">list</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">account</span> <span style="color: #339933;">=</span> list.<span style="color: #660066;">list</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>;
                k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">account</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                    username<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;justin&quot;</span><span style="color: #339933;">,</span>
                    domain<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;meetup.com&quot;</span><span style="color: #339933;">,</span>
                    displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Meetup.com&quot;</span><span style="color: #339933;">,</span>
                    dataTypes<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;CALENDAR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                    isDataReadOnly<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                    icons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>largeIcon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span> smallIcon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>;
                self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">accountServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'createAccount'</span><span style="color: #339933;">,</span>
                    parameters<span style="color: #339933;">:</span> self.<span style="color: #660066;">account</span><span style="color: #339933;">,</span>
                    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got %j for %j&quot;</span><span style="color: #339933;">,</span> response<span style="color: #339933;">,</span> self.<span style="color: #660066;">account</span><span style="color: #009900;">&#41;</span>;
                        self.<span style="color: #660066;">account</span>.<span style="color: #660066;">accountId</span> <span style="color: #339933;">=</span> response.<span style="color: #660066;">accountId</span>;
                        k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onFailure<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>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create account&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error creating account&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>You&#8217;ll notice a few different things about this code, the odd things which are just my style. Using <code>self</code> instead of <code>this</code> is an idiom from <a href="http://bitbucket.org/jmtulloss/cobra/overview/">Cobra</a>, a JavaScript class library I wrote. <code>k</code> is a continuation, or the function that should be called after the account is created. <code>k</code> I believe is an idiom for <a href="http://en.wikipedia.org/wiki/Continuation-passing_style">Continuation Passing Style</a> in <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a>.</p>
<p>Beyond that, this is very standard Mojo code. Services are identified as &#8220;palm://com.palm.<service>&#8221; and all methods support <code>onSuccess</code>, <code>onFailure</code>, and <code>onError</code> callbacks. This code checks to see if there is already an account associated with this appId, and if there is, calls its continuation. If there is not, it creates the account and calls its continuation.</p>
<p>After the account is created, we can <a href="http://developer.palm.com/index.php?option=com_content&#038;view=article&#038;id=1751">create the calendar</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">calendarServiceId</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;palm://com.palm.calendar/crud/&quot;</span>;
&nbsp;
<span style="color: #009966; font-style: italic;">/* Retrieves calendar if it exists, otherwise creates it */</span>
setupCalendar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">calendarServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'listCalendars'</span><span style="color: #339933;">,</span>
        parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            accountId<span style="color: #339933;">:</span> self.<span style="color: #660066;">account</span>.<span style="color: #660066;">accountId</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>calList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got calendar list&quot;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>calList.<span style="color: #660066;">calendars</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">calendar</span> <span style="color: #339933;">=</span> calList.<span style="color: #660066;">calendars</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>;
                k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">calendar</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Meetup.com&quot;</span>
                <span style="color: #009900;">&#125;</span>
                self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">calendarServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'createCalendar'</span><span style="color: #339933;">,</span>
                    parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                        accountId<span style="color: #339933;">:</span> self.<span style="color: #660066;">account</span>.<span style="color: #660066;">accountId</span><span style="color: #339933;">,</span>
                        calendar<span style="color: #339933;">:</span> self.<span style="color: #660066;">calendar</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        self.<span style="color: #660066;">calendar</span>.<span style="color: #660066;">calendarId</span> <span style="color: #339933;">=</span> response.<span style="color: #660066;">calendarId</span>
                        k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    onFailure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Creating calendar failed: %j&quot;</span><span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span>;
                        Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create calendar&quot;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Creating calendar failed: %j&quot;</span><span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span>;
                        Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error creating calendar&quot;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onFailure<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>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create calendar&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error creating calendar&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>This is almost identical to the account creation code, except it&#8217;s creating a calendar.</p>
<p>Now calling these functions is easy:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">setupAccount</span><span style="color: #009900;">&#40;</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>
    self.<span style="color: #660066;">setupCalendar</span><span style="color: #009900;">&#40;</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>
        self.<span style="color: #660066;">buttonModel</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>;
        self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">modelChanged</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h3>Pulling down events</h3>
<p>To get the events that are associated with the account, I am using the <a href="http://www.meetup.com/meetup_api/clients/">Meetup.com JavaScript client</a>. The client requires jQuery, but since it only requires jQuery to do JSONP, we replace that line with a Mojo call. There is no reason that we couldn&#8217;t use jQuery, but jQuery is overkill for just doing JSONP.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span>urlprefix <span style="color: #339933;">+</span> call_type <span style="color: #339933;">+</span> url<span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>callback<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> $H<span style="color: #009900;">&#40;</span>params<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toQueryString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
url <span style="color: #339933;">=</span> urlprefix <span style="color: #339933;">+</span> call_type <span style="color: #339933;">+</span> url <span style="color: #339933;">+</span> query;
Mojo.<span style="color: #660066;">loadScriptWithCallback</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> Mojo.<span style="color: #660066;">doNothing</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>After we have the Meetup client library, getting the events is easy, but a little indirect. It takes three API calls, one to get the member id, one to get every group associated with the member id, and one to get every event in all those groups. You can see the details on <a href="http://www.meetup.com/meetup_api/docs/#readmethods">Meetup&#8217;s API page</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">client</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MeetupApiClient<span style="color: #009900;">&#40;</span>Meet.<span style="color: #660066;">Auth</span>.<span style="color: #660066;">apiKey</span><span style="color: #009900;">&#41;</span>;
syncCalendar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Gets my member id</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Syncing calendar&quot;</span><span style="color: #009900;">&#41;</span>;
    self.<span style="color: #660066;">client</span>.<span style="color: #660066;">get_members</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        relation<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;self&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self._getGroups<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_getGroups<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> members<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got members&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #003366; font-weight: bold;">var</span> memberId <span style="color: #339933;">=</span> members.<span style="color: #660066;">results</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>;
    self.<span style="color: #660066;">client</span>.<span style="color: #660066;">get_groups</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        member_id<span style="color: #339933;">:</span> memberId
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self._getEvents<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_getEvents<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> groups<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got events&quot;</span><span style="color: #009900;">&#41;</span>;
    groups <span style="color: #339933;">=</span> groups.<span style="color: #660066;">results</span>;
    <span style="color: #003366; font-weight: bold;">var</span> groupString <span style="color: #339933;">=</span> groups<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>;
    <span style="color: #003366; font-weight: bold;">var</span> today <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span>; i <span style="color: #339933;">&lt;</span> groups.<span style="color: #660066;">length</span>; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        groupString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> groups<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>;
    <span style="color: #009900;">&#125;</span>
    self.<span style="color: #660066;">client</span>.<span style="color: #660066;">get_events</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        group_id<span style="color: #339933;">:</span> groupString<span style="color: #339933;">,</span>
        after<span style="color: #339933;">:</span> today.<span style="color: #660066;">getMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> today.<span style="color: #660066;">getDay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> today.<span style="color: #660066;">getFullYear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self._saveEvents<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>Now <code>self._saveEvents</code> will receive a list of events that are coming up after today. All we need to do is loop over the list, format them as Palm calendar events, and pass them to the calendar service.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">_saveEvents<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> events<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Saving events&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    self.<span style="color: #660066;">numEventsProcessed</span> <span style="color: #339933;">=</span> 0;
    self.<span style="color: #660066;">events</span> <span style="color: #339933;">=</span> events;
&nbsp;
    events.<span style="color: #660066;">results</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>meetupEvent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> time <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>meetupEvent.<span style="color: #660066;">time</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>meetupEvent.<span style="color: #660066;">myrsvp</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;no&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">calendarServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'createEvent'</span><span style="color: #339933;">,</span>
                parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    calendarId<span style="color: #339933;">:</span> self.<span style="color: #660066;">calendar</span>.<span style="color: #660066;">calendarId</span><span style="color: #339933;">,</span>
                    event<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                        eventId<span style="color: #339933;">:</span> meetupEvent.<span style="color: #660066;">id</span><span style="color: #339933;">,</span>
                        subject<span style="color: #339933;">:</span> meetupEvent.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
                        startTimestamp<span style="color: #339933;">:</span> time<span style="color: #339933;">,</span>
                        endTimestamp<span style="color: #339933;">:</span> time <span style="color: #339933;">+</span> <span style="color: #CC0000;">3600000</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// 1 hour in ms</span>
                        allDay<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                        note<span style="color: #339933;">:</span> self._formatNote<span style="color: #009900;">&#40;</span>meetupEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                        location<span style="color: #339933;">:</span> meetupEvent.<span style="color: #660066;">lat</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;, &quot;</span> <span style="color: #339933;">+</span> meetupEvent.<span style="color: #660066;">lon</span><span style="color: #339933;">,</span>
                        alarm<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                onSuccess<span style="color: #339933;">:</span> self._createdEvent<span style="color: #339933;">,</span>
                <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> self._errorCreatingEvent<span style="color: #339933;">,</span>
                onFailure<span style="color: #339933;">:</span> self._failureCreatingEvent
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_createdEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self._checkIfSyncFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_errorCreatingEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Could not create event: %j&quot;</span><span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span>;
    self._checkIfSyncFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_failureCreatingEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create event: %d, %j&quot;</span><span style="color: #339933;">,</span> self._eventsReturned<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span>;
    self._checkIfSyncFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_checkIfSyncFinished<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">numEventsProcessed</span>++;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">numEventsProcessed</span> <span style="color: #339933;">==</span> self.<span style="color: #660066;">events</span>.<span style="color: #660066;">meta</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">events</span>.<span style="color: #660066;">meta</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Fetching the next page of results...&quot;</span><span style="color: #009900;">&#41;</span>;
            self.<span style="color: #660066;">client</span>.<span style="color: #660066;">nextPage</span><span style="color: #009900;">&#40;</span>self._saveEvents<span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Fetched all the events&quot;</span><span style="color: #009900;">&#41;</span>;
            self.<span style="color: #660066;">buttonModel</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>;
            self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">modelChanged</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>;
            self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sync-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mojo</span>.<span style="color: #660066;">deactivate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>And that&#8217;s basically it! With the code above, all your Meetup.com events can be inserted into your webOS calendar. All you need is an event handler for your button.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">buttonModel</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    buttonLabel<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Sync'</span><span style="color: #339933;">,</span>
    disabled<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">setupWidget</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sync-button'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    type<span style="color: #339933;">:</span> Mojo.<span style="color: #660066;">Widget</span>.<span style="color: #660066;">activityButton</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Mojo.<span style="color: #660066;">Event</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sync-button'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Mojo.<span style="color: #660066;">Event</span>.<span style="color: #660066;">tap</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>
    self.<span style="color: #660066;">buttonModel</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>;
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">modelChanged</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sync-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mojo</span>.<span style="color: #660066;">activate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    self.<span style="color: #660066;">syncCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This works assuming a view containing:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div x-mojo-element=&quot;Button&quot; id=&quot;sync-button&quot;&gt;&lt;/div&gt;</pre></div></div>

<h3>Debugging Tips</h3>
<p>Creating a file called <code>framework_config.json</code> allows you to change the logging level. That will permit JavaScript messages to be output into /var/log/messages on the device. This is especially valuable if you&#8217;re working on a 64-bit linux machine where the inspector is currently not supported.</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{
    &quot;logLevel&quot;: 99 // 0 means no logging, 99 will max it out
}</pre></div></div>

<p>Removing /var/luna/data/dbdata/PalmDatabase.db3 removes all data you may have inserted. This allows you to start over fresh, but I wouldn&#8217;t recommend doing this on your actual device. You&#8217;ll lose all your data!</p>
<h3>Todo</h3>
<p>This program isn&#8217;t really complete, but it&#8217;s a good start. A couple of things we still have to do are:</p>
<ul>
<li>Add Authentication with OAuth</li>
<li>Keep track of already inserted appointments so we don&#8217;t insert duplicates</li>
<li>Add automatic synchronization in the background</li>
<li>Add push updates (this is impossible without server side support)</li>
<li>Make things look nice</li>
</ul>
<p>Stay tuned for articles on some or all of these exciting new features! Until then, the <a href="http://github.com/jmtulloss/Meetup.com-Mojo-Client/tree/Services">code is available on github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2009/07/meetup-com-webos-client-part-1-services/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

